Reputation: 1094
I have something like this (with many more elements in root
):
<root>
<a>
<b></b>
</a>
<a>
<b></b>
</a>
</root>
I would like to find the b
node at position i
. What I tried is something like:
findElement(By.xpath("root//b[" + i + "]"));
But this method to find nodes by position seems not to work with //
before. How can I find my node?
Upvotes: 2
Views: 391
Reputation: 111620
Note the difference between:
//b[1]
, which selects all b
elements that are first among b
element siblings because [1]
binds more tightly than //
.(//b)[1]
, which selects the first b
element among all b
elements
in the document.So, if you want the i
th b
element in the document, use
findElement(By.xpath("(//b)[" + i + "]"));
You can, of course, limit the scope to parts of the document by preceding the b
step with other steps higher in the hierarchy.
Upvotes: 4