Back
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.
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 } }
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