我想以 Angular 自动化 webtable。这是网站-http://demo.automationtesting.in/WebTable.html
我想捕获所有行的所有记录。这是我尝试过的-
List<WebElement> wbt=drv.findElements(By.cssSelector("div[role='rowgroup'][class='ui-grid-viewport ng-isolate-scope']"));
System.out.println("size"+wbt.size());
for(int i=0; i<wbt.size();i++){
System.out.println("values-"+wbt.get(i).getText());
}
但是出来的尺寸是1。
我也尝试过使用 div[class='ui-grid-contents-wrapper'],但没有成功。
最佳答案
您的选择器指向表 div 和行。要获取表格行,您可以使用 .ui-grid-row
选择器,它将为您提供表格主体行。
List<WebElement> rows = drv.findElements(By.cssSelector(".ui-grid-row"));
for (WebElement row : rows) {
System.out.println("values-" + row.getText());
}
要按行获取每个列元素,您可以在循环内使用 .ui-grid-cell-contents
选择器:
List<WebElement> rows = drv.findElements(By.cssSelector(".ui-grid-row"));
for (WebElement row : rows) {
columns = row.findElements(By.cssSelector(".ui-grid-cell-contents"));
columns.forEach(column -> System.out.println(column.getText().trim()));
}
您可以了解选择器here .
此外,您可能需要等待某些条件得到满足,您可以从以下位置获取信息:When to use explicit wait vs implicit wait in Selenium Webdriver?
关于java - 如何使用 Selenium 处理 Angular 中的 WebTables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55648726/