2017-12-02 212 views
4

我有这样的HTML页面之间选择元素:的XPath 2个的某些元素

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

我要选择带班的TR元素“other_label”这是最后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> 

回答

4

的XPath “绝招”:

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

这实际上工作! ..非常感谢 –