How to find a specific child of a specific web element with XPath selectors in Selenium

Viewed 21

In one of our Selenium integration tests we use this css selector:

webDriver.findElements(By.cssSelector(
        String.format("tr[data-qa-element='foo'] td[data-qa-element='bar']")));

I need to change this selector to an xPath expressions, because we want to uncouple it from element names like 'tr' and 'td'. How do I turn this into an xpath expression:

  • The data-qa-element of the containing element needs to be foo
  • The data-qa-element of the contained element needs to be bar
  • Find all bars that are contained withing a foo
1 Answers

Without element tag names tr and td your CSS Selector converted to XPath will be as following:

"//*[@data-qa-element='foo']//*[@data-qa-element='bar']"
Related