• 【转】XPath的学习


    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”关键字的,搜索的就是之上/之下的所有节点,忽略同级概念,例如:
    <div>
    <input id="123">
    <input>    
    </div>
    要定位第二个input://input[@id='123']/following-sibling::input
     
    2.preceding-sibling
    preceding-sibling的解释是“选取当前节点之前的所有同级节点”,那么没有加上“sibling”关键字的,搜索的就是之上/之下的所有节点,忽略同级概念,                            preceding-sibling和following-sibling是刚好相反的
    <div>
    <span>text</span>
    <input id="123">    
    </div>
    要定位第二个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:<div id="positions">
    <input>
    <span>test position()1</span>
    <span>test position()2</span>
    <span>test position()3</span>
    <span>test position()4</span>
    <span>test position()5</span>
    </input>
    </div>
    获取第一个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:
    <div id="xxx">
    <span name="?-xxxxx-09">text</span>
    </div>
    定位上面html中span://div[@id='xxx']/span[substring(@name,3,5)='xxxxx']
     
    substring-before的用法,比如html
    <div id="xxx">
    <span class="spanclass1-789">text</span>
    </div>
    定位上面html中span://div[@id="xxx"]/span[sbustring-before(@class,"-")="spanclass1"]
     
    substring-after的用法,比如html
    <div id="xxx">
    <span class="789-spanclass2">text</span>
    </div>
    定位上面html中span://div[@id="xxx"]/span[sbustring-after(@class,"-")="spanclass22"]
    通配符  *
    比如//span[@*="xxx"]指定位span中任意属性包含xxx的
    比如//*[@*="xxx"]指定位页面中任意属性保护xxx的标签
     
    Axes 轴 使用 XPath 的轴(axis)进行元素定位

    轴:
    XPath轴(XPath Axes)可定义某个相对于当前节点的节点集:
    1、child 选取当前节点的所有子元素
    2、parent 选取当前节点的父节点
    3、descendant 选取当前节点的所有后代元素(子、孙等)
    4、ancestor 选取当前节点的所有先辈(父、祖父等)
    5、descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身
    6、ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身
    7、preceding-sibling 选取当前节点的开始标签之前的所有同级节点
    8、following-sibling 选取当前节点的结束标签之后的所有同级节点
    9、preceding 选取文档中当前节点的开始标签之前的所有节点
    10、following 选取文档中当前节点的结束标签之后的所有节点
    11、self 选取当前节点
    12、attribute 选取当前节点的所有属性
    13、namespace 选取当前节点的所有命名空间节点

    使用的时候注意加上::
     

    XPath轴关键字 

    轴的含义说明 

    定位表达式示例 

    表达式解释 

    ancestor 

    选择当前节点上层的所有节点 

    //img[@alt='div2-img2']/ancestor::div 

    查找到alt属性值为div2-img的图片,并基于图片位置找到他上级的div页面元素 

    descendant 

    选择当前节点下层的所有节点

    注:不能超出当前节点的结束标签

    //div[@name='div2']/descendant::img 

    查找到name属性值的div页面元素,并基于div的位置找到他下级所有节点中的img页面元素 

    following 

    选取当前节点的结束标签之后的所有节点

    注:仔细体会与descendant的区别

    //div[@id='div1']/following::img 

    超找到id 属性值为div1的div页面元素,并基于div的位置找到它后面(结束标签之后)节点中的img页面元素 

    following-sibling 

    选取当前节点的结束标签之后的所有平级节点 

    //a[@href='http://www.sogou.com']/follo 

    wing-sibling::input 

    查找到链接地址为http://www.sogou.com的链接页面元素,并基于链接的位置找到它后续节点中input页面元素 

    preceding 

    选择当前节点开始标签之前的所有节点 

    //img[@alt='div2-img2']/preceding::div 

    查找到alt属性值为div2-img2的图片页面元素,并基于图片的位置找到它前面节点中的div页面元素 

    preceding-sibling 

    选择当前节点开始标签之前的所有同级节点 

    //img[@alt='div2-img2']/preceding-sibling 

    ::a[1] 

    查找到alt属性值为div2-img2的图片页面元素,并基于图片的位置找到它前面同级节点中的第二个链接页面元素 

     
    XPath 使用页面元素的属性值定位
     

    预期定位的页面元素 

    定位表达式示例 

      使用的属性值 

    定位页面的第一个图片 

    //img[@href='http://www.sogou.com'] 

      使用img标签的href属性值  

    定位第二个div中第一个Input输入框 

    //div[@name='div2']/input[@name='div2input'] 

      使用div标签的name属性值 

      使用 input标签的name 属性值  

    定位第一个div中的第一个链接 

    //div[@id='div1']/a[@href='http://www.sogou.com'] 

      使用的div标签的id 属性值 

      使用了a标签的href属性值  

    定位页面的查询按钮 

    //input[@type='button'] 

      使用了type属性值 

     
     
    参考:http://www.cnblogs.com/xxyBlogs/p/4244073.html
       http://www.cnblogs.com/testlife007/p/4263745.html
  • 相关阅读:
    Android系统启动过程分析
    android的logcat 用法整理
    git 使用详解(10) 远程分支
    android的logcat 用法整理
    android Binder工作流程
    android Binder工作流程
    git log 小结
    linux patch 命令小结
    windows 中 \r\n 区别于 类unix中的\n 疑问 迎刃而解
    Mysql Error Code : 1436 Thread stack overrun
  • 原文地址:https://www.cnblogs.com/hycms/p/4958900.html
Copyright © 2020-2023  润新知