c# - 访问表中的元素(Selenium,C#)
解决方案
不要使用来自 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);
}
}
推荐阅读
- ssis - 访问存储在 SSIS 杂项文件夹中的脚本文件
- swift - 如何在没有 Alamofire Swift 的情况下使用 URL Session 获得响应
- java - Avro 1.9.1 逻辑类型“uuid”映射到 java 类中的字符串类型
- python - 计算时代的平均值
- entity-framework - Scaffold-DbContext 上下文和模型上的不同命名空间
- python - Django 将项目从 Windows 主机移动到 Linux 主机(并部署)
- c++ - CToolBar MFC 64 位在 32 位上运行时崩溃
- c# - 如何将 DateTime 值序列化为“yyyy-MM”?
- python - 如何使用 python 并行执行作业?
- python - 我将如何计算我的硬件可以处理的 python 脚本的同时调用数?