XPath编写规则学习
 
辅助工具:firefox安装findbugs,view Xpath
firefox :Xpath验证方式:$x("xpath"); 粘贴xpath语句回车即可
 
定位:
1、依靠自己属性,文本定位:
   //td[text()='test']
   //div[contains(@class,'test')]
   //input[@type='radio' and @value='1']
   //span[@name='bruce' and text()='bruce2'] 或 //span[@name='bruce'][ text()='bruce2'] --and关键字
   //span[@name='bruce' or text()='bruce2'] --or关键字
 
2、依靠父节点定位:
  //div[@class='test mytest']/div
  //div[@id='test']/div
 
3、依靠子节点定位
  //div[div[@id='test']] --寻找含有id=test的div的div
  //div[div[@name='test']]
  //div[p[@id='test']]
 
4、混合型定位
  //div[div[@name='test']]/img
  //td[a//font[contains(text(),'test')]]//input[@type='checkbox']
 
5、高级方法
       
   (1)following-sibling ---寻找紧跟定位到的元素的下一个元素
   例子://input[@id='1234']/following-sibling=input --定位紧跟id=1234的下一个的input元素,同级有效
             //input[@id='1234']/following-sibling::input ,input后可再跟条件
 
   (2)preceding-sibling ---寻找紧跟定位到的元素的上一个元素
   例子://input[@id='123']/preceding-sibling=span --定位紧跟id=123的上一个span元素
             //input[@id='1234']/preceding-sibling::input ,input后可再跟条件
 
     (3)starts-with --判断是否以某关键字开头
      例子://input[starts-with(@id,'test')]
     (4)contains -- 是否包含某关键字
       例子: //td[a//font[contains(text(),'test')]]//input[@type='checkbox']
     (5)not ---不包含某关键字
        例子://input[not(@id='1234')]
                  //span[not(contaions(text(),'xpath'))]
 
6、索引关键字,position,last
    (1)position()=2
             position()>3
             position()<5
       
      例子://div[@id='test']/span[2]或
                //div[@id='test']/span[position()=2] --正数第2个span
 
      (2)last()-1
 
       例子://div[@id='test']/span[last()-2] --倒数第2个span元素
 
7、根据属性定位
  //div[@class] --查找含有class属性的div
  //div[@class='test'] --查找含有class属性且class属性值为test的的div元素
 
8、不常用关键字
     (1)substring,语法:substring(str,start_postion,length) ,从1开始计算
           例子://div[@id='test']/span[substring(@name,3,5)='bruce'] --找name的第三位开始总共5位字母为bruce的span
 
     (2)substring-before ,语法:substring-before(str,substr)
           例子://div[@id='test']/span[substring-before(@class,'-')='spanclass'] --查找分割关键字前面的字符为spanclass的span
 
     (3)substring-after,语法:substring-after(str,substr)
            例子://div[@id='substring']/span[substring-after(@class,'-')='spanclass'] --查找分割关键字后面的字符为spanclass的span
 
9、通配符:*
      //span[@*='bruce']
      //*[@*='bruce']
      //*[@name='bruce']
 
10、axes 轴
   (1)parent 父节点
          例子://div[span[text()='+++test']]/parent::div[contaions(text(),'test')] --查找含有span的text为+++test的的div的父节点
                    //div[span[text()='+++test']]/parent::div/span[contaions(text(),'test')]
 
   (2)ancestor 祖先节点
          例子://div[span[text()='+++test']]/ancestor::div
     
   (3)descendant 孙子节点
           例子://div[span[text()='+++test']]/descendant::div --会将该节点下的所有div打印出来
                      //div[span[text()='+++test']]/descendant::div/span[contaions(text(),'test')]
 
   (4)following 将当前节点下后面所有的指定节点取出
            例子://div[text()='current NodeA']/following::div --会将current NodeA后面的所有的div取出来,后续的div可再加条件判断
       
   (5)preceding 将当前节点下前面所有的指定节点取出
            例子://div[text()='current NodeA']/preceding::div --会将current NodeA前面的所有的div取出来,后续的div可再加条件判断

最新文章

  1. apr 内存管理
  2. Creating SharePoint 2010 Event Receivers in Visual Studio 2010
  3. Java 单链表的倒置
  4. C语言中.h和.c文件解析
  5. Dijkstra算法and Floyd算法 HDU 1874 畅通工程续
  6. 《Programming WPF》翻译 第6章 3.二进制资源
  7. hdu 5282 Senior&amp;#39;s String 两次dp
  8. GameUnity 2.0 文档(三) 纸片人八方向
  9. Java 9 揭秘(11. Java Shell)
  10. git bash上传代码到github
  11. [LeetCode] Valid Palindrome II 验证回文字符串之二
  12. 服务注册中心之ZooKeeper系列(二) 实现一个简单微服务之间调用的例子
  13. sublime快捷方式
  14. elk收集windows日志
  15. 虚拟机和主机ping不通解决的方法
  16. javascript中的this到底指什么?
  17. numpy.pad
  18. 学习Android Studio里的Gradle
  19. Open vSwitch for CentOS
  20. Python——极限编程

热门文章

  1. Atitit jquery &#160;1.4--v1.11 &#160;v1.12 &#160;v2.0 &#160;3.0 的新特性
  2. mysql中tinyint、smallint、int、bigint的区别
  3. C++编程学习50个经典网站 强力推荐(转载)
  4. HTML5学习笔记(十四):变量作用域
  5. .NET MVC5+ Dapper+扩展+AutoFac自动注入实现
  6. tensorflow笔记3:CRF函数:tf.contrib.crf.crf_log_likelihood()
  7. Asp.Net时间方法大全
  8. Java Hex 16进制的 byte String 转换类
  9. SQL 迭代查询语句
  10. JS 对应CSS 样式