Reputation: 2051
We can find element by inner text, for example:
<span>
<div>
Example Text
</div>
<div>
Other Text
</div>
</span>
In this case we can use follow xpath: //span/div[contains(text(), 'Example')]
But how can I found element when the text exist out of the tag?
For example:
<span>
<div id="1">
...........
</div>
Example Text
<div id="2">
......
</div>
Other Text
</span>
And shat if, I haven't id
on the div tags, and I can't use order?
<span>
...........
<div>
...........
</div>
Example Text
<div>
......
</div>
Other Text
</span>
Upvotes: 1
Views: 2486
Reputation: 1135
You can use xpath axes to select siblings.
//text()[contains(.,"Example")]//preceding-sibling::div[1]
Upvotes: 2
Reputation: 33361
You can use this XPath
//span[contains(text(),'Example Text')]//div[@id='1']
For the first div
.
The same for the second div
//span[contains(text(),'Example Text')]//div[@id='2']
Generally, in this case the "Example Text" text is contained by the parent span
element.
So you can locate the parent span
according to this text and then drill down to the child div
.
Upvotes: 1