0

我发现一个表格元素使用

WebElement tableElement = selectorboxelement.findElement(By.xpath("//ancestor::table"));

selectorboxelement 是我通过使用找到的 webelement

WebElement selectorboxelement = driver.findelement(By.Name("p_part_chkbox"));

我这样做的原因是因为我找不到表格的 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>
4

1 回答 1

1

从中获取行:

List<WebElement> tableRows = table.findElements(By.xpath("tr"));

然后表格单元格:

List<WebElement> tableCells= table.findElements(By.xpath("tr/td"));

或 - 在 for 循环中遍历所有 tableRows 并通过以下方式获取单元格

for(WebElement row : tableRows) {
    List<WebElement> cells = row.findElements(By.xpath("td"));
   }

最后,您可以通过使用获取单元格中的值

for(WebElement cell : tableCells) {
    System.out.println( "Value = " + cell.getText());
}
于 2014-11-03T16:26:47.953 回答