简体   繁体   中英

Java. Selenium webdriver. Get cell value from table

Here's a piece of my 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>

So what I need is to retrieve cell values. One more thing is that when viewing code via browser's source code option it looks same as above. But when using Firefox's firebug it additionally shows that values are inside of

<span></span>

tag

I know that here xPath is the solution. For the past 2 hours I've tried so many but none of it worked. And of course I've seen similar questions' solutions on SO but somehow they are not suitable for me.

What I'm trying to do:

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());
        }
    }
/**
 * 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;
}

Locate the td elements and call .getText() method:

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());
    }
}

Here, we are using CSS selectors to locate table rows and then row cells for every table row.

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());
   }
}

try to find your table by:

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

debug it and find your table after that you can do the upper code:

for example something like...

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

To complement the previous awnser, if you need get the texts from a specific row, you can do this way:

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());
}

In this case, I'm using td[2] to get the second column of the row and compare the username. You can chage it if desire compare another fields.

Also, you can assert if the xpath selector is working or if it is returning all that you want using the chrome console:

$x("your xpath selector here")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM