java - Selenium - 如何动态计算表中的行数?

标签 java selenium-webdriver

在我的 html 页面中有一个包含 10 行的表格;这些行将根据过滤器(动态)显示。比方说,默认情况下没有任何过滤器将返回 10 行。 应用过滤器后,将返回少于 10 行,具体取决于过滤器的类型,因此我想使用 selenium 网络驱动程序获取表行的动态计数(过滤器后)。

我尝试使用 driver.findElements(By.xpath("abcd")).size() 但这是默认计数 10;然而,在应用过滤器之后,只出现了 2 行。

请建议如何获取动态计数(count=2 表示在 UI 中出现 2 行)。

最佳答案

要找到动态网页上的元素总数,我们需要使用 driver.findElements().size() 方法。但有时它根本没用。

首先获取与行数匹配的所有元素的大小。一旦我们有了它,您就可以使用动态 xpath,即替换行号和列号运行时来获取数据。

{
    List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
    //To calculate no of rows In table.
    int rows_count = rows_table.size();

    //Loop will execute till the last row of table.
    for (int row=0; row<rows_count; row++){
    //To locate columns(cells) of that specific row.
    List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
    //To calculate no of columns(cells) In that specific row.
    int columns_count = Columns_row.size();
    System.out.println("Number of cells In Row "+row+" are "+columns_count);

    //Loop will execute till the last cell of that specific row.
    for (int column=0; column<columns_count; column++){
    //To retrieve text from that specific cell.
        String celtext = Columns_row.get(column).getText();
        System.out.println("Cell Value Of row number "+row+" and column number "+column+" Is "+celtext);
    }
    System.out.println("--------------------------------------------------");
}

关于java - Selenium - 如何动态计算表中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33445575/

相关文章:

css - 如何定位列表中定义的按钮?

ruby - 协议(protocol).rb :153:in `read_nonblock' : end of file reached (EOFError)

java 打印数组并出现 Arrays.toString() 错误

java - 对于多人游戏场景,如何在客户端之间序列化更新消息?

java - 如何用JDBC查找已删除记录的主键?

selenium - 使用 Robot Framework 执行 selenium 示例时出错

java - Selenium 2 hub 启动但在尝试访问控制台时抛出 404

java - ZXing 2.0发布与在线ZXing解码器有什么不同吗?

java - 使用数组中的参数进行 Vararg 初始化 (Java)

python - 使用请求 python 在谷歌上进行简单查询时出现错误 429