You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用XPath定位目标节点的下一个同级兄弟节点

解决方案

要定位<div class="indent" id="dir_2567698_short" style="display: none;">的下一个同级兄弟节点,直接用XPath的following-sibling轴即可,正确写法如下:

精准定位第一个同级div节点

//*[contains(@class,'indent') and contains(@style,'display: none')]/following-sibling::div[1]

匹配任意类型的第一个同级节点

如果不确定目标节点的标签类型,用:

//*[contains(@class,'indent') and contains(@style,'display: none')]/following-sibling::*[1]

你的方法无效的原因

你之前写的/./div[1]是在当前节点的子节点里查找元素,而你要找的是同级兄弟节点,路径方向完全错误,所以无法定位到目标。

进阶精准匹配

如果目标节点有专属特征(比如特定class),可以进一步缩小范围,比如假设目标节点也带indent类:

//*[contains(@class,'indent') and contains(@style,'display: none')]/following-sibling::div[contains(@class,'indent')][1]

内容的提问来源于stack exchange,提问作者bokabokaboka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 06:12:39