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()]