当前节点的 select 下一个父节点的 Xpath

Xpath to select next parent of the current node

如果 tr 包含 class="productnamecolor colors_productname" 我想 select 下一个包含价格详细信息的 tr。所以我使用:

.//a[@class="productnamecolor colors_productname"]/parent::node()/following-sibling::tr

但是没用。这个表达式有什么问题?

HTML :

<tr> 
 <td valign="top" width="100%">
  <a href="unclesamsretailoutlet.com/Trouser-Suspenders-8440015269920-p/…; class="productnamecolor colors_productname" title="Trouser Suspenders, 2323">Trouser Suspenders</a> 
  </td>
</tr>

提前致谢。

您的 <a> 元素的父元素是 td 元素,并且 td 元素没有跟随兄弟元素 - 当然不是 td 元素的跟随兄弟元素=15=]。如果您想要 table 中的下一行,请使用

.//a[@class="..."]/ancestor::tr[1]/following-sibling::tr[1]

.//tr[descendant::a/@class="..."]/following-sibling::tr[1]

如果你想 select 在 <a class="productnamecolor colors_productname"> 之后的下一个 tr 只需使用以下两种方式:-

  • 使用following轴:

    (.//a[@class="productnamecolor colors_productname"]/following::tr)[1]
    
  • 使用preceding 轴:

    (.//tr[preceding::a[@class="productnamecolor colors_productname"])[1]
    

希望对您有所帮助...:)