在我的 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/