美文网首页
xpath 定位方式

xpath 定位方式

作者: 口袋妖精_b8d0 | 来源:发表于2018-05-06 18:42 被阅读0次

    xpath的作用就是两个字“定位”,运用各种方法进行快速准确的定位,推荐两个非常有用的的firefox工具:firebug和xpath checker

    定位

    1.依靠自己属性,文本定位

    //td[text()='xxx']

    //div[contains(@class,'xxx')]

    //div[@class='xxx' and @type='xxx']

    2.依靠父节点定位

    //div[@class='xxx']/div

    //div[@id='xxx']/div

    3.依靠子节点定位

    //div[div[@id='xxx']]

    //div[div[@name='xxx']]

    4.混合型

    //div[div[@name='xxx']]/img

    //td[a/font[contains(text(),'xxx')]]//input[@type='xxx']

    xpath的学习-拓展

    1.following-sibling

    following-sibling即为“选择当前节点之后的所有同级节点”,那么没有加上“sibling”关键字的,搜索的就是之上/之下的所有节点,忽略同级概念,例如:

       

    要定位第二个input://input[@id='123']/following-sibling::input

    2.preceding-sibling

    preceding-sibling的解释是“选取当前节点之前的所有同级节点”,那么没有加上“sibling”关键字的,搜索的就是之上/之下的所有节点,忽略同级概念,                            preceding-sibling和following-sibling是刚好相反的

    text

       

    要定位第二个input://input[@id='123']/preceding-sibling::span

    3.contains

    和字面意思一样就是包含,例如://div[contains(@class,'xxx')]

    4.starts-with

    和字面意思一样就是以某某开头,例如://input[starts-with(@class,'xxx')]

    5.not

    就是否定的意思

    比如找一个id不为123的input:input[not[id='123']]

    又如找一个文本中不包含xxx字段的span://span[not(contains(text(),'xxx'))]

    xpath的学习-补充

    绝对路径html/body/div/span[2]/input[2] 中间结构变化,就失效

    相对路径//开始,在整个html source里找,不管在什么位置

    索引[x]//div/input[2] div下面第二个input

    position()=2position()>3position()<3

    例如html:

    test position()1

    test position()2

    test position()3

    test position()4

    test position()5

    获取第一个span,可以是//div[@id='positions']/span[1],也可以是//div[@id='positions']/span[position()=1]

    //div[@id='positions']/span[position()>3]就是定位了test position()4和test position()5

    //div[@id='positions']/span[position()<3]就是定位了test position()1和test position()2

    last()last()-1

    以上面的html为例子,获取最后一个span://div[@id='positions']/span[last()]

    以上面的html为例子,获取倒数第二个span://div[@id='positions']/span[last()-1]

    属性定位@class   //div[@class]  有class属性的div

    属性值定位,前面已经讲过了  //div[@class='xxx']

    功能关键字

    1.常用

    and/[][],比如://span[@name='xxx' and text()='xxx']也是可以写成//span[@name='xxx'][text()='xxx']

    or,比如以上面html为例子,定位文本为test position()5和test position()4的span://div[@id='positions']/span[text()='test position()5' or text()='test position()4']

    not,contains,starts-with

    ends-with 在xpath中是没有这个的

    2.不常用的

    substring,substring-before,substing-after

    sbustring(str,start-position,length)比如html:

    text

    定位上面html中span://div[@id='xxx']/span[substring(@name,3,5)='xxxxx']

    substring-before的用法,比如html

    text

    定位上面html中span://div[@id="xxx"]/span[sbustring-before(@class,"-")="spanclass1"]

    substring-after的用法,比如html

    text

    定位上面html中span://div[@id="xxx"]/span[sbustring-after(@class,"-")="spanclass22"]

    通配符  *

    比如//span[@*="xxx"]指定位span中任意属性包含xxx的

    比如//*[@*="xxx"]指定位页面中任意属性保护xxx的标签

    Axes 轴

    parent 父节点

    ancestor 祖先节点,包括父节点,一层一层向上

    descendant 所有子孙节点找,不管什么位置,简写//,就是xpath中出现//的情况。。//div[@class="xxx"]//input

    follwing-sibling 当前元素后面的兄弟姐妹

    preceding-sibling 当前元素前面的兄弟姐妹

    following 当前元素后面所有元素,一直到

    preceding  当前元素之前所有元素,一直到

    ancestor-or-self

    descendant-or-self

    使用的时候注意加上::

    相关文章

      网友评论

          本文标题:xpath 定位方式

          本文链接:https://www.haomeiwen.com/subject/ilwurftx.html