In my html page there's a table with 10 rows; those rows will display based on filter (dynamically). Let's say, for example, without any filters by default 10 row will be returned. after applying the filter, less than 10 rows will be returned, depending on the type of filter, so i want to get that dynamic count of table rows (after filter) using selenium web driver.
i tried with driver.findElements(By.xpath("abcd")).size()
but this is giving default count 10; however, after applying the filter only 2 rows are appearing.
Please suggest how to get dynamic count (count=2 as appearing 2 rows in UI) .
答案 0 :(得分: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("--------------------------------------------------");
}
答案 1 :(得分:0)
我将此作为我不是Java dev的事实的前缀,但我在其他语言中使用了很多webdriver。
设置测试的最佳方法是等待测试操作的结果。有时候你可以在没有等待或定时等待的情况下离开,但是然后你就可以在一个较慢的网格盒上运行你的测试,所有东西都堆在一堆。
类似&#34; div存在&#34;,&#34; div有类&#34;,无论结果如何。在您的情况下,听起来您可能无法测试要渲染的div,但您可以使用您的大小测试作为等待的结果。
Selenium可以使用任何ExpectedCondition,也可以指定函数
WebDriverWait wait = new WebDriverWait(getDriver(), 5);
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
int elementCount = driver.findElement(By.xpath("xxxx")).size();
if (elementCount == 2)
return true;
else
return false;
}
});