【问题标题】:How to get the last row number of table which is dynamically updating?如何获取动态更新的表的最后一行?
【发布时间】:2016-08-20 09:09:17
【问题描述】:

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

假设有一个 3*3 的表格。在那个单元格(2,3)单元格文本是“测试”。那么如何使用单元格文本“Test”找到行号 = 2。

硒-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/font

其中粗体 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>

【问题讨论】:

  • 能否分享您的表格 HTML 以便更好地理解??
  • 是的..我已经添加了
  • 所以这里要根据文本搜索行号。对吧??
  • 是的..我想在文本的基础上点击td(这里是td with href="t.php")。
  • 基于文本或获取该表最后一行的索引(正在更新,即行正在动态添加到该表中)。这样我就可以点击最后一行

标签: java selenium selenium-webdriver html-table


【解决方案1】:

"count(//table[@id='tableid']//td[.='Test']/parent::tr/preceding-sibling::tr) + 1"

放入您自己的表标识符...取决于它是一个没有行跨度和列跨度的简单表。

【讨论】:

  • 让我试试...这是我的 xpath "//table[2]/tbody/tr/td[2]/table[2]/tbody/tr[7]/td/div [2]/table/tbody/tr[1]/td[1]/div/a/font" 我正在使用代码 driver.findElement(By.xpath("//table[2]/tbody/tr/ td[2]/table[2]/tbody/tr[7]/td/div[2]/table/tbody/tr[1]/td[1]/div/a/font")).click() ;
  • 也没有表ID或类
  • 分享相关表格的html页面源代码。
【解决方案2】:

我尝试将所有行的列数据捕获到一个列表中(观察下面'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++;    
}

}
}

【讨论】:

    【解决方案3】:

    如果你想在文字的基础上点击&lt;td&gt;,试试下面xpath:-

    .//td[child::a/font[text() = 'your text hare']]
    

    或者如果你点击&lt;td&gt;里面的&lt;a&gt;,根据文字试试下面xpath:-

    .//td/a[child::font[text() = 'your text hare']]
    

    或者你想click on table cell using their row and column index follow this link

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签