如何获取动态更新的表的最后一行?

阿迪蒂亚

我们如何使用其包含的值获取Table(在网页中)行。

假设一个3 * 3的表。在该单元格(2,3)中,单元格文本为“测试”。因此,如何使用单元格文本“ Test”查找行号= 2。

Selenium-2.53.1 Java-8

这是xpath

// table [2] / tbody / tr / td [2] / table [2] / tbody / tr [7] / td / div [2] / table / tbody / tr [1] / td [1] / div / a /字体

其中粗体tr(此处为tr [1])是我要动态更改的行,并且文本位于粗体td(此处为td [1])内

我的html代码是:

<html>
<head></head>
<body>
<form name="fm" method="post">
 <table>
    <!-- --Some data--- -->
 </table> 

  <table width="100%" height="100%" border="0" >
  <tr valign="top" > 
  <td width="200" height="100%" rowspan="3"  align="center" > 
        <p></p>
        <table width="89%" height="270" border="0">
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <!-- --Some data--- -->
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr> 
       <td><table width="100%" height="71" border="0" align="right">
        <tr></tr>
        <tr></tr>
        <tr></tr>
        <tr></tr>
       </table></td>
    </tr>

    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <!-- --Some data--- -->
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    <tr></tr>
    </table>
    <p></p>
  </td>  
  <td width="622" height="30" colspan="3" align="left" valign="top">
    <table width="100%" height="40" align="center">
          <tr><!-- --Some data--- --></tr>
    </table>
        <table width="476" height="110" align="center">
    <tr></tr>
    <tr></tr>
    <tr></tr>  
    <!-- --Some data--- -->
    <tr></tr>     
    <tr></tr>
    <tr></tr>
    <tr> 
       <td height="22" colspan="3">
          <div align="left"> 
        <table width="469" border="1">
                  <tr bgcolor="#006291"> 
                     <td width="81" bgcolor="#006291"><div align="center">SlNo.</div></td>
                     <td width="82" bgcolor="#006291"><div align="center">Code</div></td>
                     <td width="300" bgcolor="#006291"><div align="center">Name</div></td>
          </tr>
                </table>
              </div>
              <div align="left" style="width:465px;Height:150px;"> 
        <table width="465" bgcolor="#FFFFFF" border="1">
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >1</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >01</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=1"><font color="#006291" size="3" >tvm1</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#F1F8FC' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >2</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >02</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=2"><font color="#006291" size="3" >tvm2</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >3</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >03</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=3"><font color="#006291" size="3" >tvm3</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#F1F8FC' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >4</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >04</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=4"><font color="#006291" size="3" >tvm4</font></a></div></td>
        </tr>
        <tr bordercolor="#006291" bgcolor='#FFEADF' >
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >5</font></a></div></td>
          <td width="81" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >10</font></a></div></td>
          <td width="300" height='5' valign="top"><div align="center"><a href="t.php?Tcode=66"><font color="#006291" size="3" >tvm5</font></a></div></td>
        </tr>                
        </table>
              </div>
       </td>
    </tr>
    </table>

  </td>
  </tr>
  </table>
</form>
</body>
</html>
普拉珊斯

我尝试将所有行的列数据捕获到一个列表中(观察下面的“ rowList”的xpath,没有给出最后的“ tr”的数字,该数字获取所有行的列数据),并使用foreach获取该行的索引其中包含必需的文本。

将不得不更改“ expRowValue”中的值以使其与其他数据一起使用。

public class Table {
WebDriver driver; 

By rowList = By.xpath("html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[1]/table[1]/tbody/tr[3]/td/table/tbody/tr/td[2]");

  @Test
  public void f() {
  String expRowValue="$513";

System.setProperty("webdriver.chrome.driver", "C:/Users/dell/Downloads/chromedriver/chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com/");
driver.manage().window().maximize();

List<WebElement>rowsData =  driver.findElements(rowList);
int k=1;    
for (WebElement rowValue: rowsData) {
    if (rowValue.getText().equals(expRowValue)){
        System.out.println("ROw Index::" + k);
    }
k++;    
}

}
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章