Home > Software design >  Selenium: How to get td objects within tr objects
Selenium: How to get td objects within tr objects

Time:04-17

I am trying to get rows of information for a user using Selenium and xpaths. I am able to get the rows using the following code:

String xpath = "//tbody[contains(@class,'svelte-abc')]//tr";
List<WebElement> elements = webDriver.findElements(By.xpath(xpath));

What I am not sure is how to then parse the individual elements (TD's) for the row, e.g. the first one is a name, the second is email address.

The html is:

<table >
    <thead>
    .....
    </thead>

    <tbody>
        <tr >
            <td >
                <div >
                    <img ... >
                </div>
            </td>
            <td >A Person</td>
            <td >[email protected]</td>
        </tr>
    </tbody>

CodePudding user response:

The desired information is distributed among several <td>s. So you have to identify the specific <td>s

To parse the texts you can use the following locator strategies:

  • Printing A Person:

    System.out.println(driver.findElement(By.xpath("//table[@class='svelte-abc']//tbody/tr[@class='svelte-abc']//following::td[2]")).getText());
    
  • Printing [email protected]:

    System.out.println(driver.findElement(By.xpath("//table[@class='svelte-abc']//tbody/tr[@class='svelte-abc']//following::td[3]")).getText());
    
  • Related