I'm trying to get all the elements inside a selenium WebElement, for example:
<div>
<element>
<header>header</header>
<p>Paragraph</p>
</element>
<element>
<p>Another Paragraph</p>
</element>
</div>
I want to get a list of all the elements inside including child elements, like so...
- div
- element
- header
- paragraph
- element
- paragraph
So I've tried this
element.findElements(By.xpath("*"))
but this would return only
- element
- element
So then I tried a recursive method to get all of the childs
private static ArrayList<WebElement> getAllElements(WebElement element, ArrayList<WebElement> ret) {
ret.add(element);
List<WebElement> childs = element.findElements(By.xpath("*"));
for(WebElement e: childs)
getAllElements(e,ret);
return ret;
}
private static List<WebElement> getAllElements(WebElement element) {
return getAllElements(element,new ArrayList<WebElement>());
}
But querying findElements() takes too long
I'm thinking there must be a xpath way, but I can't seem to find it