xpath轴定位

本文详细介绍了XPath的各种轴定位,包括ancestor、ancestor-or-self、attribute、child、descendant等,并给出了具体的使用示例,涉及文本节点定位、属性定位、组合定位等多种应用场景,帮助读者深入理解XPath在网页元素查找中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.xpath通配符定位

//*[@id="org-tree"]     //*[@*="org-tree"]

 

2.xpath轴介绍

ancestor:当前节点的所有先辈

ancestor-or-self:当前节点的所有先辈及自身

attribute:当前节点的所有属性

child:当前节点的所有子元素

descendant:当前节点的所有后代元素

descendant-or-self:当前节点的所有后代元素及节点自身

following:当前节点结束标签之后的所有节点

following-sibling:当前节点之后的所有弟弟节点

namespace:当前节点的所有命名空间节点

parent:当前节点的父节点

preceding:当前节点开始标签之前的所有节点

preceding-sibling:当前节点之前的所有同级节点

self:自身节点

child ::text():当前节点的所有文本子节点

child ::node():当前节点的所有子节点

拓展:starts-with:匹配一个属性开始位置的关键字

 

3.xpath轴具体使用

//*[@*="current"]/child ::text()       //*[@class="current"]/child ::text()       //*[@class="current"]/child ::node()

//*[@id="org-tree"]/child ::*/child ::*      //*[@id="org-tree"]/child ::*/child ::div

拓展://指全文上下文中搜索//后面的节点,而.//指从当前节点之后的子节点中进行查找,.表示当前节点。

          凡是用text()地方和.等价。

.//*[@id="org-tree"]/child ::*/child ::div        //div[./div[@id="org-tree"]]/child ::*/child ::div

//div[.//div[@id="org-tree"]]/child ::*/child ::div

//div[.//div[@class="el-tree-node__content"]]//span[.//span[@class="current"]]

//div[.//div[@class="el-tree-node__content"]]//span[./span[@class="current"]]

//div[./div[@class="el-tree-node__content"]]//span[./span[@class="current"]]

//span[.="查 询"]       //div[./div[@class="el-tree-node__content"]]/descendant ::*

 

4.定位方式

(1)ancestor定位

//div[@class="el-tree-node__content"]/ancestor ::*       //div[@class="el-tree-node__content"]//ancestor ::div

(2)ancestor-or-self定位

//div[@class="el-tree-node__content"]//ancestor-or-self ::*      //div[@class="el-tree-node__content"]/ancestor-or-self ::div

(3)attribute定位

//div[@class="el-row"]/attribute ::*       //div[@class="el-row"]/attribute ::class     //div[@class="el-row"]/attribute ::style

(4)child定位

//div[@class="el-row"]/child ::div    //div[./div[@class="el-row"]]/child ::div

(5)descendant定位

//div[./div[@class="el-row"]]/descendant ::div

(6)descendant-or-self定位

//div[./div[@class="el-row"]]/descendant-or-self ::div

(7)following定位

//div[./div[@class="el-row"]]/following ::div      //td[@class="available today"]/following ::td[@class="available"]

//div[./div[@class="el-row"]]/following ::div//div[./div[@class="el-row"]]/following ::div/following ::div

//td[@class="available today"]/following ::td[@class="next-month"][last()]

//button[@aria-label="下个月"]/../following ::div/table/tbody/tr[6]/td[@class="available"][last()]

(//input[@placeholder="年/月/日"])[2]/../following ::input

(8)following-sibling定位

//div[./div[@class="el-row"]]/following ::div/following-sibling ::div/following-sibling ::div

//table/colgroup/following-sibling ::tbody

(9)parent定位

//div[./div[@class="custom-tree-node"]]//parent ::*/parent ::div

(10)preceding定位

//div[./div[@class="custom-tree-node"]]//preceding ::*/parent ::div/preceding ::div

//td[@class="next-month"]/preceding ::td[@class="available"][last()]

(11)preceding-sibling定位

//div[./div[@class="custom-tree-node"]]//preceding-sibling ::*/parent ::div/preceding ::div

(12)self定位

//div[./div[@class="custom-tree-node"]]//self ::*/self ::div/self ::div

(13)child ::text()定位

//div[./div[@class="custom-tree-node"]]//child ::text()

(14)child ::node()定位

//div[./div[@class="custom-tree-node"]]//child ::node()

(15)starts-with定位

//div[starts-with(@class,"custom-tree-node")]//child ::node() 

表示查找class属性中开始位置包含"custom-tree-node"关键字的页面元素及其当前节点的所有子节点

//span[starts-with(text(),"查 询")]   表示查找text文本中开始位置包含"查 询"关键字的span标签

 

5.高级组合定位

查找日历盘每个月tr[6]、tr[7]最后一个可用日期

//button[@aria-label="下个月"]/../following ::div/table/tbody/tr[5]/following ::tr[@class="el-date-table__row"]/td[@class="available"][last()]

查找日历盘每个月的最后一天

(//button[@aria-label="下个月"]/../following ::div/table/tbody/tr[5]/following ::tr[@class="el-date-table__row"]/td[@class="available"])[last()]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值