2017-12-02 266 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

這實際上工作! ..非常感謝 –