Reputation: 2732
Given following markup
<div>
<a>Username1</a>
</div>
<div>
<button>Unblock</button>
</div>
<div>
<a>Username2</a>
</div>
<div>
<button>Unblock</button>
</div>
<div>
<a>Username3</a>
</div>
<div>
<button>Unblock</button>
</div>
How do I select button element which is a cousin of a
element with text Username2?
I can select the a
element with //a[contains(., 'Username2')]
, so I thought that //a[contains(., 'Username2')]/following-sibling::/div/button
would select the correct button, but that does not work. I think that it's not even valid XPATH.
Upvotes: 3
Views: 2879
Reputation: 338376
You were close:
//a[contains(., 'Username2')]/../following-sibling::div[1]/button
To navigate to the cousin you first have to go to the parent (..
) and then to its sibling.
Note that the following-sibling::
axis selects all following siblings, not only the first one. This means you must use [1]
if you just want the first.
This would also work:
//a[. = 'Username2']/../following-sibling::div[1]/button
So would this:
//div[a = 'Username2']/following-sibling::div[1]/button
Upvotes: 8