请勿使用来自chrome的绝对xpath,因为这很可能会失败。处理表时,最佳实践是访问table> tr> td。下面是可以帮助您的示例。这可以作为起点,然后根据单元格文本单击元素。
//find table.
WebElement mytable = driver.findElement(By.xpath("html/body/table/tbody"));
//find rows of table.
List < WebElement > rows_table = mytable.findElements(By.tagName("tr"));
//calculate no of rows In table.
int rows_count = rows_table.size();
//Loop will execute for all the rows of the table
for (int row = 0; row < rows_count; row++) {
//find columns(cells) of that specific row.
List < WebElement > Columns_row = rows_table.get(row).findElements(By.tagName("td"));
//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 the cells.
String celltext = Columns_row.get(column).getText();
System.out.println("Cell Value Of row number " + row + " and column number " + column + " Is " + celltext);
}
}