zoukankan      html  css  js  c++  java
  • XPath编写规则学习

    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可再加条件判断
  • 相关阅读:
    你真的知道async/await的好处嘛, 并且还会用好呢
    Python基本小程序
    猜数字小程序的实现
    Python第一周基本语句学习整理
    Python环境安装与配置
    作业
    Markdown的学习
    创建一个dynamics CRM workflow (四)
    Dynamics CRM 快速获取custom entity
    Dynamics email的subject标题出现 CRM:0000xxxx
  • 原文地址:https://www.cnblogs.com/guanzelin/p/8677618.html
Copyright © 2011-2022 走看看