【问题标题】:Xpath to select next parent of the current nodeXpath 选择当前节点的下一个父节点
【发布时间】:2016-07-31 08:49:16
【问题描述】:

如果 tr 包含 class="productnamecolor colors_productname" 我想选择下一个包含价格详细信息的 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>

提前感谢。

【问题讨论】:

标签: xpath import.io


【解决方案1】:

&lt;a&gt; 元素的父元素是td 元素,而td 元素没有follow-sibling - 当然不是tr 的following 兄弟。如果您想要表格中的下一行,请使用

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

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

【讨论】:

  • @Arunkumar:如果有帮助,请accept这个答案。谢谢。
  • 先生有没有办法选择第 n 个元素。像 tr[3],tr[6],tr[9]。因为包含价格详情的是tr[3],tr[6],tr[9]
  • @Arunkumar 只需将 xpath 中的索引从 1 更改为您想要的...:)
  • @SaurabhGaur 我可以使用 (2n+1)。我想要该页面中的所有列表。
  • @Arunkumar 是的,你可以。但是根据 xpath,在提供行详细信息后,您只会获得所有行..:)
【解决方案2】:

如果您想在&lt;a class="productnamecolor colors_productname"&gt; 之后选择下一个tr,只需使用以下两种方式:-

  • 使用following 轴:

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

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

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

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签