我一直在尝试一个任务,该表包含三行七列,每列包含一个整数,目的是通过Selenium读取表(DOM)并提取每一行每一列的整数值,必须使用所有这些整数创建一个数组。最后,我确实每三个元素都有三个数组(a [],b [],c [])。如果有人有一个好的开始来实现这个任务,将不胜感激。
<tr style="border-bottom: 1px solid rgb(224, 224, 224); color: rgba(0, 0, 0, 0.87); height: 48px;">
<td data-test-id="array-item-1-0" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">23</td>
<td data-test-id="array-item-1-1" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">50</td>
<td data-test-id="array-item-1-2" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">63</td>
<td data-test-id="array-item-1-3" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">90</td>
<td data-test-id="array-item-1-4" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">10</td>
<td data-test-id="array-item-1-5" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">30</td>
<td data-test-id="array-item-1-6" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">155</td>
<td data-test-id="array-item-1-7" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">23</td>
<td data-test-id="array-item-1-8" style="padding-left: 24px; padding-right: 24px; height: 48px;
text-align: left; font-size: 13px; overflow: hidden; white-space: nowrap; text-overflow:
ellipsis; background-color: inherit;">18</td>
</tr>
答案 0 :(得分:0)
import org.apache.commons.lang3.ArrayUtils;
WebElement table_or_tbody = driver.findElement(<locator of your table or tbody>);
// find all table rows
List<WebElement> rows = table_or_tbody.findElements(By.cssSelector("tr"));
List<String[]> table_data = new ArrayList<String[]>();
for(WebElement row:rows) {
// find all cells of each table row
List<WebElement> cells = row.findElements(by.cssSelector("td"));
String[] texts = new String[0];
// read text of each cell
for(WebElement cell:cells) {
texts = ArrayUtils.add(texts, cell.getText().trim());
}
table_data.add(texts);
}
String[] a = table_data.get(0);
String[] b = table_data.get(1);
String[] c = table_data.get(2);
Maven pom.xml添加以下依赖项:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>