Search code examples
javaseleniumselenium-chromedriverdescendantfindelement

How can I find all of the descendants with a specific attribute of an web element using ChromeDriver


While trying to figure out how to locate a specific group of web elements, I came across this question and I was wondering if I can do the same action just that it would give me only the descendants elements that contain a specific attribute.

For clarification, I know that I might be able to filter it but I'm looking for a way to do it without filtering.

Furthermore, I'm using java, and as web driver I use chrome driver (and I use Selenium for that, I don't know if it's obvious or not as that's the only web controlling tool that I learned so far...)

Github link

Thanks for the answers ahead!


Solution

  • To find elements with attribute that contains 'attributeValue':

    List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*[contains(@attributeName,'attributeValue')]"));
    

    To find elements with attribute that fully matches 'attributeValue':

    List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*[@attributeName='attributeValue']"));