gpt4 book ai didi

java - 如何用Java与网页上的表格进行交互?

转载 作者:行者123 更新时间:2023-12-01 12:20:42 25 4
gpt4 key购买 nike

我正在尝试制作一个工作程序来自动化我们维护的电子表格。这些信息位于我们登录的第三方网站上,然后滚动浏览一堆页面,然后就会出现一个包含所有信息的表格。我使用 selenium webdriver 打开 Internet Explorer,登录,然后导航到正确的页面,但我不知道如何与表交互。当我在 Google Chrome 中检查元素时,表格或其任何单元格都没有显示 ID 或名称。提前谢谢大家。

编辑:这是 HTML 代码。我为敏感信息添加了星号。带星号的信息是我需要提取的信息。我只发布了代码的第一季度左右,因为它的字符太多,但代码只是重复更多行。

    <HTML>
<HEAD>
<TITLE>***************</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function to_SchedForm(theForm) {
var j = 0;
if (theForm.p_chkbox_cnt.value == 1)
{
if (theForm.p_part_chkbox.checked ){
++j;
}
}
else{
for (var i = 0; i < theForm.p_part_chkbox.length; i++ ) {
if (theForm.p_part_chkbox[i].checked ){
++j;
}
}
}
if (j == 0) {
alert("No Part Numbers Selected");
return false;
}
return true;
}
function to_MultiSchedForm(theForm) {
var j = 0;
var c = 0;
var k = 0;
for (var i = 0; i < theForm.p_part_chkbox.length; i++ ) {
if (theForm.p_part_chkbox[i].checked ){++k;
if (theForm.p_mode_chk[i].value != "CR"){
++j;
}
}
}
if (k == 0) {
alert("No Part Numbers Selected")
return false;
}
if (j != 0 && theForm.ACTION1.value == "multi_order") {
alert("The Multiple Schedule Update option is only available for Schedules that currently have a blank Last Commit value. Use the Individual Schedules Button to change a commitment Date.")
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE BORDER="0" WIDTH="100%"
CELLSPACING="0" CELLPADDING="0">
<TR>
<TD WIDTH="100%", VALIGN="top"><IMG SRC="/apps/space/img/PPPS_NEW.jpg" ALT="GM Banner" NAME="PPPS1"></TD>
</TR>
<TR>
<TD width="50%"><HR></TD>
</TR>
</TABLE>
<TABLE BORDER="0" WIDTH="100%" CELLSPACING="0"
CELLPADDING="0">
<TR>
<TD WIDTH="100%", VALIGN="top", ALIGN="center"><IMG SRC="/apps/space/img/priority3.jpg" ALT="Priority Banner" NAME="Priority1"></TD>
</TR>
</TABLE>
<BR>
<CENTER>
<TABLE WIDTH="100%">
<TR>
<FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to1_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_onum" VALUE="">
<INPUT TYPE="hidden" NAME="p_proc" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="c_sort_one" VALUE="part_number">
<INPUT TYPE="hidden" NAME="c_sort_two" VALUE="priority">
<INPUT TYPE="hidden" NAME="c_sort_three" VALUE="commit_mode">
<INPUT TYPE="hidden" NAME="c_sort_four" VALUE="po_rel_sort">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="none">
<TD VALIGN="BOTTOM" ALIGN="LEFT"><FONT FACE="courierNew" SIZE="2"><B> Part #:</B><INPUT TYPE="text" NAME="p_pnum" SIZE="12" MAXLENGTH="12"></FONT><INPUT TYPE="submit" VALUE="GO"></TD>
</FORM>
<FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to2_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_pnum" VALUE="">
<INPUT TYPE="hidden" NAME="p_proc" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="c_sort_one" VALUE="po_rel_sort">
<INPUT TYPE="hidden" NAME="c_sort_two" VALUE="part_number">
<INPUT TYPE="hidden" NAME="c_sort_three" VALUE="priority">
<INPUT TYPE="hidden" NAME="c_sort_four" VALUE="commit_mode">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="none">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<TD VALIGN="BOTTOM" ALIGN="LEFT"><FONT FACE="courierNew" SIZE="2"><B> Order #: </B><INPUT TYPE="text" NAME="p_onum" SIZE="12" MAXLENGTH="12"></FONT><INPUT TYPE="submit" VALUE="GO"></TD>
</FORM>
<FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to3_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_pnum" VALUE="">
<INPUT TYPE="hidden" NAME="p_onum" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="c_sort_one" VALUE="po_rel_sort">
<INPUT TYPE="hidden" NAME="c_sort_two" VALUE="part_number">
<INPUT TYPE="hidden" NAME="c_sort_three" VALUE="priority">
<INPUT TYPE="hidden" NAME="c_sort_four" VALUE="commit_mode">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="none">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<TD VALIGN="BOTTOM" ALIGN="LEFT"><FONT FACE="courierNew" SIZE="2"><B><SELECT NAME="p_proc"><OPTION VALUE="0">&lt; Select Processor &gt;<OPTION VALUE="KXH">K.Hr 918040497243 <OPTION VALUE="SJF">S. Fox 586-484-0855 </SELECT></B></FONT><INPUT TYPE="submit" VALUE="GO"></TD>
</FORM>
</TR>
</TABLE>
</CENTER>
<HR>
<CENTER>
<FONT FACE="courierNew" SIZE="2">Mfg. DUNS: <B>178522926</B></FONT>
&nbsp;&nbsp;
<FONT FACE="courierNew" SIZE="2"><B>KAUTEX INC</B></FONT>
&nbsp;&nbsp;
<FONT FACE="courierNew" SIZE="2">Z-code: <B>BL62</B></FONT>
</CENTER>
<CENTER>
<TABLE WIDTH="100%" BORDER="2" BORDERCOLOR="#00008B">
<TR>
<TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Select One or More</FONT></B></FONT></CENTER></TH><FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to4_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="iv_group" VALUE="">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns_number" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="p_pnum" VALUE="">
<INPUT TYPE="hidden" NAME="p_onum" VALUE="">
<INPUT TYPE="hidden" NAME="p_proc" VALUE="">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="part_num">
<INPUT TYPE="hidden" NAME="c_sort_one" VALUE="part_number">
<INPUT TYPE="hidden" NAME="c_sort_two" VALUE="priority">
<INPUT TYPE="hidden" NAME="c_sort_three" VALUE="commit_mode">
<INPUT TYPE="hidden" NAME="c_sort_four" VALUE="po_rel_sort">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<TH BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><INPUT TYPE="image" NAME="p_img" SRC="/apps/space/img/Part_Number_Button2.gif" border="0"></B></FONT></CENTER></TH></FORM>
<TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="21%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Description</FONT></B></FONT></CENTER></TH><FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to5_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="iv_group" VALUE="">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns_number" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="p_pnum" VALUE="">
<INPUT TYPE="hidden" NAME="p_onum" VALUE="">
<INPUT TYPE="hidden" NAME="p_proc" VALUE="">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="ordr_num">
<INPUT TYPE="hidden" NAME="c_sort_one" VALUE="po_rel_sort">
<INPUT TYPE="hidden" NAME="c_sort_two" VALUE="part_number">
<INPUT TYPE="hidden" NAME="c_sort_three" VALUE="priority">
<INPUT TYPE="hidden" NAME="c_sort_four" VALUE="commit_mode">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<TH BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><INPUT TYPE="image" NAME="p_img" SRC="/apps/space/img/Order_Number_Button2.gif" border="0"></B></FONT></CENTER></TH></FORM>
<TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="7%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Last Commit</FONT></B></FONT></CENTER></TH><FORM ACTION="Schedule.PrioritySummary" METHOD="POST" NAME="to6_PS_Page">
<INPUT TYPE="hidden" NAME="P_PERSON_ID" VALUE="BZRGJH">
<INPUT TYPE="hidden" NAME="iv_group" VALUE="">
<INPUT TYPE="hidden" NAME="p_mode" VALUE="EDIT">
<INPUT TYPE="hidden" NAME="p_type" VALUE="SCHEDULE">
<INPUT TYPE="hidden" NAME="p_space_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns_number" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcode" VALUE="BL62">
<INPUT TYPE="hidden" NAME="p_pnum" VALUE="">
<INPUT TYPE="hidden" NAME="p_onum" VALUE="">
<INPUT TYPE="hidden" NAME="p_proc" VALUE="">
<INPUT TYPE="hidden" NAME="p_sort_type" VALUE="none">
<INPUT TYPE="hidden" NAME="p_supplier" VALUE="">
<INPUT TYPE="hidden" NAME="p_zcodes_space" VALUE="">
<INPUT TYPE="hidden" NAME="p_duns" VALUE="">
<INPUT TYPE="hidden" NAME="p_part" VALUE="">
<TH BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><INPUT TYPE="image" NAME="p_img" SRC="/apps/space/img/Due_Now_Button2.gif" border="0"></B></FONT></CENTER></TH></FORM>
<TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Due 11/03</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Due 11/10</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Due 11/17</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Due 11/24</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Future</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">Total</FONT></B></FONT></CENTER></TH><TH VALIGN="CENTER" ALIGN="CENTER" WIDTH="5%" BGCOLOR="darkblue"><CENTER><FONT COLOR="WHITE" SIZE="-1"><B><FONT FACE="courierNew" SIZE="2">SNR</FONT></B></FONT></CENTER></TH></TR>
<FORM ACTION="Schedule.SchedForm" METHOD="POST" NAME="toSchedForm"onSubmit="if (this.ACTION1.value == 'single_order')
{
if (to_SchedForm(this))
{
return true
}
else
{
return false
}
}
else
{
if (to_MultiSchedForm(this))
{
document.toSchedForm.action=&quot;Schedule.multi_schedform&quot;;return true
}
else
{
return false
}
return false
}">
<INPUT TYPE="hidden" NAME="P_EMS_DIVISION" VALUE="M ">
<INPUT TYPE="hidden" NAME="P_DOC_PREFIX" VALUE="XQ">
<INPUT TYPE="hidden" NAME="P_DOC_NUMBER" VALUE="APHL ">
<INPUT TYPE="hidden" NAME="P_DOCUMENT_TYPE" VALUE="Q">
<INPUT TYPE="hidden" NAME="P_PART_DLS" VALUE="A">
<INPUT TYPE="hidden" NAME="P_PART_PLS" VALUE=" ">
<INPUT TYPE="hidden" NAME="P_PART_NUM" VALUE="23214821">
<INPUT TYPE="hidden" NAME="p_mode_chk" VALUE="CR">
<INPUT TYPE="hidden" NAME="p_chkbox_cnt" VALUE="1">
<TR>
<TD VALIGN="CENTER" ALIGN="CENTER"><B><INPUT TYPE="checkbox" NAME="p_part_chkbox" VALUE="1"></B></TD>
<TD VALIGN="CENTER"
ALIGN="RIGHT"><FONT FACE="CourierNew" SIZE="2"><B>******** </B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="LEFT"><FONT FACE="courierNew" SIZE="2">TANK ASM - FUEL </FONT></TD>
<TD><FONT FACE="CourierNew" SIZE="2">*******</FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>-</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><B><FONT COLOR="*****" FACE="courierNew" SIZE="2"></B><B>12</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>****</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>-</B></FONT></TD>
</TR>
<INPUT TYPE="hidden" NAME="P_EMS_DIVISION" VALUE="M ">
<INPUT TYPE="hidden" NAME="P_DOC_PREFIX" VALUE="XQ">
<INPUT TYPE="hidden" NAME="P_DOC_NUMBER" VALUE="APHL ">
<INPUT TYPE="hidden" NAME="P_DOCUMENT_TYPE" VALUE="Q">
<INPUT TYPE="hidden" NAME="P_PART_DLS" VALUE="A">
<INPUT TYPE="hidden" NAME="P_PART_PLS" VALUE=" ">
<INPUT TYPE="hidden" NAME="P_PART_NUM" VALUE="***********">
<INPUT TYPE="hidden" NAME="p_mode_chk" VALUE="CR">
<INPUT TYPE="hidden" NAME="p_chkbox_cnt" VALUE="2">
<TR>
<TD VALIGN="CENTER" ALIGN="CENTER"><B><INPUT TYPE="checkbox" NAME="p_part_chkbox" VALUE="2"></B></TD>
<TD VALIGN="CENTER"
ALIGN="RIGHT"><FONT FACE="CourierNew" SIZE="2"><B>******** </B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="LEFT"><FONT FACE="courierNew" SIZE="2">TANK ASM - FUEL </FONT></TD>
<TD><FONT FACE="CourierNew" SIZE="2">J1UJ3 -000</FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>-</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><B><FONT COLOR="FF0000" FACE="courierNew" SIZE="2"></B><B>*****</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>0</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>********</B></FONT></TD>
<TD VALIGN="CENTER"
ALIGN="CENTER"><FONT FACE="courierNew" SIZE="2"><B>-</B></FONT></TD>

最佳答案

好的,知道可以通过元素 id 找到任何元素是很有用的,例如driver.findElement(By.id("table"))。这将为您提供该类型的第一个元素。但是,您有多个表,因此您需要找到一种方法来获取正确的表。

有不同的方法可以做到这一点,但它们通常会围绕目标的某些个人特征进行。在这种情况下,对我来说最明显的例子是它包含以下内容:

<TR>
<TD WIDTH="100%", VALIGN="top", ALIGN="center"><IMG SRC="/apps/space/img/priority3.jpg" ALT="Priority Banner" NAME="Priority1"></TD>
</TR>

尽管这意味着我们正在降低 child 水平(使事情变得更复杂)。我们能做的就是查找该特定元素,然后通过 xpath 找到其父表,如 this xpath tutorial 所示。 。

  1. 因此,要查找图像元素,我们只需使用:

WebElement imageElement = driver.findElement(By.name("Priority1"));

这给了我们图像元素。

  • 要查找包含所述 imageElement 的表,我们可以使用:
  • WebElement tableElement = imageElement.findElement(By.xpath(//ancestor::table));

    这应该找到祖先元素,它是包含优先级 1 图像的表。

    我建议你看看 x-path 是如何工作的。它非常有用,但有时确实需要一些修改(特别是当网页发生变化时)。

    关于java - 如何用Java与网页上的表格进行交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26676748/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com