我正在使用 Java 和 Selenium 编写测试。在我的目标应用程序 DOM 中,有很多我需要遍历的表。我曾经使用过这样的东西:
WebElement mytable = driver.findElement(By.xpath(".//table/tbody"));
List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
int rows_count = rows_table.size();
for (int row=0; row<rows_count; row++){
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
int columns_count = Columns_row.size();
System.out.println("Number of cells In Row "+row+" are "+columns_count);
for (int column=0; column<columns_count; column++){
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) selenium.getTable
2) GetTable(ElementFinder 查找器,JavascriptLibrary js)
但我找不到任何好的代码示例。
长话短说,我想知道是否有比查找 .//tr
或 .//td
更好的方法来处理表中的行和列?
最佳答案
getTable 未在 webdriver 中实现。不过你可以试试这个。
WebDriver driver = new ChromeDriver();
Selenium selenium = new WebDriverBackedSelenium(driver, "your website url");
selenium .getTable("xpath");
selenium.getTable("table.1.2"); getTable(tableCellAddress) 参数:
tableCellAddress - a cell address, e.g. "foo.1.4"
Returns: the text from the specified cell
Gets the text from a cell of a table. The cellAddress syntax tableLocator.row.column, where row and column start at 0.
为该方法添加抑制警告。
关于java - 如何处理表格的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39209932/