SeleniumWebDriver-使用Java8从数据表中获取列

SeleniumWebDriver-使用Java8从数据表中获取列,java,selenium,Java,Selenium,我试图从数据表中获取一列。 这是我的表作为一个例子,我要寻找的是从表中提取Firstname <table style="width:100%"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr> <tr> <td class="abc">Jill&

我试图从数据表中获取一列。 这是我的表作为一个例子,我要寻找的是从表中提取Firstname

<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th> 
    <th>Age</th>
  </tr>
  <tr>
    <td class="abc">Jill</td>
    <td class="abc">Smith</td> 
    <td class="abc">50</td>
  </tr>
  <tr>
    <td class="abc">Eve</td>
    <td class="abc">Jackson</td> 
    <td class="abc">94</td>
  </tr>
</table>

名字
姓氏
年龄
吉尔
史密斯
50
前夕
杰克逊
94
如何修改下面的代码以获得此结果:

Jill
Eve 


WebElement table = driver.findElement(By.id("searchResultsGrid"));

// Now get all the TR elements from the table
List<WebElement> allRows = table.findElements(By.tagName("tr"));
// And iterate over them, getting the cells
for (WebElement row : allRows) {
    List<WebElement> cells = row.findElements(By.tagName("td"));
    for (WebElement cell : cells) {
        System.out.println("content >>   " + cell.getText());
    }
}
吉尔 前夕 WebElement table=driver.findElement(By.id(“searchResultsGrid”); //现在从表中获取所有TR元素 List allRows=table.findElements(按.tagName(“tr”)); //然后迭代它们,得到细胞 for(WebElement行:所有行){ 列表单元格=行.findElements(按.tagName(“td”)); for(WebElement单元:单元){ System.out.println(“content>>”+cell.getText()); } }
使用
Java 8
您可以在仅获取
Firstname
列列表后使用迭代列表,如下所示:-

WebElement table = driver.findElement(By.id("searchResultsGrid"));

List<WebElement> firstCells = table.findElements(By.xpath(".//tr/td[1]"));
firstCells.forEach(firstCell->System.out.println("Firstname >>   " + firstCell.getText()));
WebElement table=driver.findElement(By.id(“searchResultsGrid”);
List firstCells=table.findElements(By.xpath(“.//tr/td[1]”);
forEach(firstCell->System.out.println(“Firstname>>”+firstCell.getText());

使用
Java 8
您可以在仅获取
Firstname
列列表后使用迭代列表,如下所示:-

WebElement table = driver.findElement(By.id("searchResultsGrid"));

List<WebElement> firstCells = table.findElements(By.xpath(".//tr/td[1]"));
firstCells.forEach(firstCell->System.out.println("Firstname >>   " + firstCell.getText()));
WebElement table=driver.findElement(By.id(“searchResultsGrid”);
List firstCells=table.findElements(By.xpath(“.//tr/td[1]”);
forEach(firstCell->System.out.println(“Firstname>>”+firstCell.getText());

我有一个div ID,我用它来访问我想要的表,使用这个类是不好的做法,我从那里得到了它。我开始阅读有关如何构建自定义xpath的书籍,这使我的生活变得更加轻松。感谢您一如既往的投入。我有div ID,我用它来访问我想要的表,使用这个类是不好的做法,我从那里得到了它。我开始阅读有关如何构建自定义xpath的书籍,这使我的生活变得更加轻松。感谢您一如既往的投入。