java Selenium - 如何动态计算表中的行数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33445575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - How to count the number of rows in a table dynamically?
提问by unknown
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.
在我的 html 页面中有一个 10 行的表格;这些行将根据过滤器(动态)显示。例如,假设没有任何过滤器,默认情况下将返回 10 行。应用过滤器后,将返回少于 10 行,具体取决于过滤器的类型,因此我想使用 selenium Web 驱动程序获取表行(过滤器后)的动态计数。
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.
我试过了,driver.findElements(By.xpath("abcd")).size()
但这给出了默认计数 10;但是,在应用过滤器后,只出现 2 行。
Please suggest how to get dynamic count (count=2 as appearing 2 rows in UI) .
请建议如何获得动态计数(计数 = 2 出现在 UI 中的 2 行)。
回答by amogh
To find the total number of elements on dynamic webpage we need to use driver.findElements().size() method. But sometimes it's not useful at all.
要查找动态网页上的元素总数,我们需要使用 driver.findElements().size() 方法。但有时它根本没有用。
First get the size of all element matching with the row count. Once we have it then you can use dynamic xpath ie replace row and column number run time to get the data.
首先获取与行数匹配的所有元素的大小。一旦我们有了它,您就可以使用动态 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("--------------------------------------------------");
}
回答by Matt
I'll prefix this with the fact that I'm not a Java dev but I use a lot of webdriver in other languages.
我将在前面说明我不是 Java 开发人员,但我使用了很多其他语言的 webdriver。
The best way to setup tests is to wait for the outcome of the action that your testing. Sometimes you can get away without doing waits, or timed waits, but then you go and run your tests on a slower grid box and everything falls in a heap.
设置测试的最佳方法是等待您的测试操作的结果。有时,您无需等待或定时等待就可以离开,但随后您会在较慢的网格框上运行测试,结果一切都堆积如山。
Things like "div exists", "div has class", whatever your outcome may be. In your case, it sounds like you may not be able to test for a div to be rendered but you can probably use your size test as the outcome to wait for.
无论结果如何,诸如“div 存在”、“div 具有类”之类的东西。在您的情况下,听起来您可能无法测试要呈现的 div,但您可能可以使用大小测试作为等待的结果。
Selenium can use any ExpectedConditionor you can specify a function
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;
}
});
Code from https://sqa.stackexchange.com/a/8701