【发布时间】:2017-07-14 02:07:17
【问题描述】:
我正在尝试将 HTML 表中的数据存储到二维数组中,但我似乎无法弄清楚。我的代码目前看起来像这样。
这是表所在的站点:https://www.w3schools.com/html/html_tables.asp
@Test
public void checkTable() {
// Number of Rows in table
int rowCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr")).size();
System.out.println("The total number of rows is " + rowCount);
// Number of Columns in table
int colCount = driver.findElements(By.xpath("//*[@id='customers']/tbody/tr[1]/th")).size();
System.out.println("The total number of columns is " + colCount);
// Dynamic X-Path
String firstX = "//*[@id='customers']/tbody/tr[";
String secondX = "]/td[";
String thirdX = "]";
for(int i=2; i <= rowCount; i++) {
for(int j=1; j <= colCount; j++) {
String completeX = firstX + i + secondX + j + thirdX;
String tableData = driver.findElement(By.xpath(completeX)).getText();
System.out.println(tableData + " ");
}
System.out.println(" ");
}
这是当前的输出。
阿尔弗雷德·富特基斯特
玛丽亚·安德斯
德国Centro comercial Moctezuma
弗朗西斯科·张
墨西哥恩斯特·亨德尔
罗兰·孟德尔
奥地利岛屿贸易
海伦·贝内特
英国笑着的巴克斯酒窖
Yoshi Tannamuri
加拿大Magazzini Alimentari Riuniti
乔瓦尼·罗维利
意大利
我试图让输出看起来像这样。
[[Alfreds Futterkiste,Maria Anders,德国],[Centro comercial Moctezuma,Francisco Chang,墨西哥]]...等。
【问题讨论】: