Let’s say I have some html that looks like this:
<section>
<header>
<h1>Foo</h1>
</header>
<section>
<a href="#">one</a>
</section>
<section>
<a href="#">two</a>
</section>
</section>
<section>
<header>
<h1>Bar</h1>
</header>
<section>
<a href="#">one</a>
</section>
<section>
<a href="#">two</a>
</section>
</section>
I want to get the anchor with the text of ‘two’ but must belong in the section tag that has the header of ‘Bar’ (i.e. the last anchor in the above html).
It’s easy to get the header element with the Bar text:
//header/h1[text()='Bar']
It’s also easy to get both anchors with the text ‘two’ that live in a section element:
//section/a[text()='two']
which of course returns two anchors.
I’m just not sure how to combine the two so that I would only got the anchor found to be in the section element that’s a sibling of the header element that contains ‘Bar’.
1 Answer