访问表(Selenium,C#)中的元素

问题描述 投票:-2回答:1

我正在尝试使工作任务自动化,这需要我访问嵌套表中的元素。我设法通过Chrome获得了XPath,但是却收到了无法找到该元素的错误。

错误消息:enter image description here

c# selenium
1个回答
0
投票

请勿使用来自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);
}
}
© www.soinside.com 2019 - 2024. All rights reserved.