findelements(by.xpath()) returning only one index of my list

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?

Answers


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.


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.


Need Your Help

Entity Framework 5.x 6.x Cache Frameworks

c# entity-framework caching redis memcached

I am using EF5 in my current project and I will upgrade it to EF6. I know that EF has an internal query cache. This is all good, but I think it won't be enough for my needs. I want to use a cache s...