Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

There is this element that has child elements, those child elements again have child elements and so on. I would like to get all the elements that are descendants of the element. Thanks.

1 Answer

0 votes
by (62.9k points)

You can try the below code, instead of using XPath since it will be useful in dynamic variables also.

         String searchText = "your element's text";

        WebElement dropdown = driver("parent locator").findElement(By.tagName("ul"));

        List<WebElement> options = dropdown.findElements(By.tagName("li"));

        System.out.println(options);

        for (WebElement option : options)

        {

            if (option.getText().equals(searchText))

            {

                option.click(); // click the desired option

            }

        } 

Hope this helps!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...