XPath选择2个特定元素之间的元素

时间:2017-12-02 20:03:47

标签: html xpath

我有这个HTML页面:

<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>

我想选择带有“other_label”类的tr元素,它们位于最后2个tr元素之间,类为“facts_label”

我试过了:

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

但这就是我得到的

<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>

1 个答案:

答案 0 :(得分:4)

Xpath &#34;技巧&#34;:

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