XPath selects items between two specific items

I have this html page:

<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>
<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>
<tr class="other_label"></tr>
<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>    # Select this
<tr class="other_label"></tr>    # Select this
<tr class="facts_label"></tr>
<tr class="other_label"></tr>
<tr class="other_label"></tr>

I want to select tr elements with class "other_label" that are between the last two tr elements with class "Facts_label"

I tried:

'//tr[@class="other_label" and (preceding-sibling::tr[@class="facts_label"][last()-1]) and (following-sibling::tr[@class="facts_label"][last()])]'

But this is what I got

<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>    # Got this
<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>    # Got this
<tr class="other_label"></tr>    # Got this
<tr class="facts_label"></tr>
<tr class="facts_label"></tr>
<tr class="other_label"></tr>    # Got this
<tr class="other_label"></tr>    # Got this
<tr class="facts_label"></tr>
<tr class="other_label"></tr>
<tr class="other_label"></tr>
+4
source share
1 answer

Xpath "trick":

//tr[ @class='other_label' and count(following-sibling::tr[@class='facts_label'])=1 ]
+4
source

Source: https://habr.com/ru/post/1690182/


All Articles