Reputation: 95
The line of code I have here is:
List<WebElement> element = driver.findElements(By.xpath("*"));
for(int i=0; i<element.size(); i++)
{
System.out.println(i + element.get(i).getText());
}
For some reason when iteratiing through the list it gives me back all the elements in one index of the list. In other words the size of the element is only 1.
How can I return the elements into multiple indexes of the list?
Upvotes: 1
Views: 1631
Reputation: 1320
By "*" you selected root HTML tag that's why count = 1. Try just for test eg. "//div"(if you need to use XPath) and see what you get.
Upvotes: 0
Reputation: 101680
If driver
is positioned at the root of the document, then *
is only going to match one element, the one at the top. If you want to select all of the elements in the entire document, then this would be the correct XPath to do that:
List<WebElement> element = driver.findElements(By.xpath("//*"));
Note that the string value of an HTML element is the combination of the text it contains, plus the text of its descendants, all the way down recursively. For example in this:
<p>
<b>Here is some bold <i>italic</i> text.</b>
Here is some normal text.
</p>
The string value of the p
element is:
Here is some bold italic text.
Here is some normal text.
The string value of the b
element is:
Here is some bold italic text.
And the string value of the i
element is: italic
.
So if you are going to actually select all of the elements and print out their content, you are going to encounter some repetition.
Upvotes: 1