据我所知,您似乎已经构建了一个自定义表格。
从所附图像中的 HTML 摘录来看,结构类似于:
<div class="ag-body-container" ...>
<div class="row_1_class" ...>
<div class="column_1_class" ...>
<div class="column_2_class" ...>
<div class="column_3_class" ...>
<div class="column_4_class" ...>
... etc
<div class="row_2_class" ...>
<div class="column_1_class" ...>
<div class="column_2_class" ...>
<div class="column_3_class" ...>
<div class="column_4_class" ...>
... etc
但是您的 xPath 假设您有表格行(我猜之后可能是表格单元格):
By.xpath("//div[@class='ag-row ag-row-even ag-row-level-0']//tr")
导致您的数组为空(很有趣,您没有得到NoSuchElement 异常,也许在您的html 树中某处有一些tr 标签)。
现在,我不确定您要从该表中提取什么数据,但您最好的尝试是根据 class 属性获取所有行,并为每一行获取所有列数据同样基于class 属性(或者您甚至可以使用col 属性)。
编辑:
要获取所有元素,您可以获取所有行,然后为每一行获取所有列数据:
//Get all the rows from the table
List<WebElement> rows = driver.findElements(By.xpath("//div[contains(@class, 'ag-row')));
//Initialize a new array list to store the text
List<String> tableData = new ArrayList<String>();
//For each row, get the column data and store into the tableData object
for (int i=0; i < rows.size(); i++) {
//Since you also have some span tags inside (and maybe something else)
//we first get the div columns
WebElement tableCell = rows.get(i).findElements(By.xpath("//div[contains(@class, 'ag-cell')]"));
tableData.add(tableCell.get(0).getText());
}
您还可以将数据存储到双向数组(或任何此类)中,然后根据行号和列号位置访问数据。