java selenium Web驱动程序从表中获取单元格值

qzwqbdag  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(434)

这是我的一段html

<table class="table_results data ajax" data-uniqueId="20605">
  <thead>
  </thead>
  <tbody>
    <tr class="odd">
     <td data-decimals="0" data-type="int" class="right data  not_null  nowrap">1</td>
     <td data-decimals="0" data-type="string" data-originallength="5" class="data  not_null   text ">user1</td>
     <td data-decimals="0" data-type="string" data-originallength="40" class="data  not_null   text ">e38ad214943daad1d64c102faec29de4afe9da3d</td>
     <td data-decimals="0" data-type="string" data-originallength="14" class="data  not_null   text ">user1@mail.com</td>
     <td data-decimals="0" data-type="string" data-originallength="6" class="data  not_null   text ">Smith</td>
     <td data-decimals="0" data-type="string" data-originallength="1" class="data  not_null   text ">&quot;</td>
    </tr>
  </tbody>
</table>

所以我需要的是检索单元格值。还有一点是,当通过浏览器的源代码选项查看代码时,它看起来和上面的一样。但是当使用firefox的firebug时,它还显示值在

<span></span>

标签
我知道xpath就是解决方案。在过去的两个小时里,我试了很多次,但都没有成功。当然,我也见过类似问题的解决方法,但不知怎么的,它们并不适合我。
我想做的是:

WebElement table_element = driver.findElement(By.xpath("//table//tbody"));
    ArrayList<WebElement> rows = (ArrayList<WebElement>) table_element.findElements(By.tagName("tr"));
    for (WebElement row : rows) {
        ArrayList<WebElement> cells = (ArrayList<WebElement>) row.findElements(By.tagName("//td"));
        for (WebElement cell : cells) {
            System.out.println(cell.getText());
        }
    }
x7yiwoj4

x7yiwoj41#

/**
 * match a string to the text of a table cell and return that row.
 *
 * @param table the table the data is in
 * @param cellTextEquals the text to identify the row e.g. unique ID
 * @param intCellToFind the column index in which to look for that text
 * @return table row
 */
protected WebElement getRowFromTable(WebElement table,
        String cellTextEquals, int intCellToFind) {
    explicitWaitForElementToBeVisibleAndClickable(table);
    WebElement tableBody = table.findElement(By.tagName("tbody"));
    List<WebElement> rows = tableBody.findElements(By.tagName("tr"));
    for (WebElement row : rows) {
        List<WebElement> td = row.findElements(By.tagName("td"));
        if (td.size() > 0
                && td.get(intCellToFind).getText().equals(cellTextEquals)) {
            return row;
        }
    }
    return null;
}

/**
 * match a string to the text of a cell and return the text of 
 * another specified cell in that row
 *
 * @param table the table the data is in
 * @param cellTextEquals the text to identify the row e.g. unique ID
 * @param intCellToFind the column index in which to look for that text
 * @param intCellToReturn the column index of the cell text you want to return
 * @return text of a specific cell in an html table
 */
protected String getTextFromTableCell(WebElement table, String cellTextEquals,
        int intCellToFind, int intCellToReturn) {
    WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind);
    List<WebElement> td = row.findElements(By.tagName("td"));
    if (td.get(intCellToFind).getText().equals(cellTextEquals)) {
        return td.get(intCellToReturn).getText();
    }
    return null;
}

/**
 * Gets an element from a table, either button or tagName, from a table.
 *
 * @param table the table the data is in
 * @param cellTextEquals the text to identify the row e.g. unique ID
 * @param intCellToFind the column index in which to look for that text
 * @param intCellToReturn the column index of the cell where the element is you want to return
 * @param buttonText the button text if it's a button element you want
 * @param tagName the tag name of the element i=icon, a=link etc.
 * @return Element By.tagName or button
 */
protected WebElement getElementFromTableCell(WebElement table,
                                             String cellTextEquals, int intCellToFind, int intCellToReturn,
                                             String buttonText, String tagName) {
    WebElement row = getRowFromTable(table, cellTextEquals, intCellToFind);
    if (row != null) {
        List<WebElement> td = row.findElements(By.tagName("td"));
        if (td.get(intCellToFind).getText().equals(cellTextEquals)) {
            if (!buttonText.equals("")) {
                return td.get(intCellToReturn).findElement(By.xpath(
                        "//button[contains(text(),'" + buttonText + "')]"));
            } else {
                return td.get(intCellToReturn)
                        .findElement(By.tagName(tagName));
            }
        }
    }
    return null;
}
svmlkihl

svmlkihl2#

找到 td 元素和调用 .getText() 方法:

List<WebElement> rows = driver.findElements(By.cssSelector("table.table_results tr"));
for (WebElement row: rows) {
    List<WebElement> cells = row.findElements(By.cssSelector("td.data"));
    for (WebElement cell: cells) {
        System.out.println(cell.getText());
    }
}

在这里,我们使用css选择器来定位表行,然后定位每个表行的行单元格。

8gsdolmq

8gsdolmq3#

List<WebElement> TRCollection = driver.findElement(By.className("table_results data ajax")).findElements(By.tagName("tr"));

for (WebElement tr : TRCollection) 
{
   List<WebElement> TDCollection = tr.findElements(By.tagName("td"));
   for (WebElement td: TDCollection) 
   {
      System.out.println(td.getText());
   }
}

尝试通过以下方式找到您的table:

List<WebElement> tables = driver.findElements(By.tagName("table"));

调试并找到表,然后执行上面的代码:
比如说。。。

List<WebElement> TRCollection = tables[2].findElements(By.tagName("tr"));
gr8qqesn

gr8qqesn4#

为了补充前面的内容,如果需要从特定行获取文本,可以通过以下方式:

string userName = "user1";
WebElement row = driver.findElement(By.xPath("//table[contains(@class, 'table_results')]/tbody/tr/td[2][text()='" + userName + "']/.."));

List<WebElement> cells = row.findElements(By.cssSelector("td.data"));
for (WebElement cell: cells) {
    System.out.println(cell.getText());
}

在这种情况下,我使用 td[2] 获取行的第二列并比较用户名。如果你想比较其他领域,你可以改变它。
此外,您还可以Assertxpath选择器是否正在工作,或者是否正在使用chrome控制台返回您想要的所有内容:

$x("your xpath selector here")

相关问题