1

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 class="svelte-abc">
    <thead>
    .....
    </thead>

    <tbody>
        <tr class="svelte-abc">
            <td class="def">
                <div class="ace">
                    <img ... >
                </div>
            </td>
            <td class="def">A Person</td>
            <td class="def">email@address.com</td>
        </tr>
    </tbody>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Swatcat
  • 73
  • 6
  • 21
  • 57

1 Answers1

1

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@address.com:

    System.out.println(driver.findElement(By.xpath("//table[@class='svelte-abc']//tbody/tr[@class='svelte-abc']//following::td[3]")).getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • This returns all of the users information as individual TD's, when you have multiple users this doesn't feel tidy. Is there a way to get a row and then findelement within that row? – Swatcat Apr 18 '22 at 08:37