Xpath 与Css 定位方式的比较

1. CSS locator比XPath locator速度快,特别是在IE下面(IE没有本身的XPath 解析器(Parser))css

2. 对于文本的处理xpath更强大使用, text()匹配的是显示文本信息。函数

String locator_Xpath = "//*[contains(text(),'test')]";

  但须要注意的是text()获取的是当前元素的文本,不包括其子元素的文本。以下代码text()获得的结果是"Please click here"。但若是使用$("#id1").text()获取的是"Memo Please click here",使用selenium获取元素text也是"Memo Please click here"。spa

<div id="id1">
    <span>Memo<span> Please click here </div>

 

3. 对于class属性Css能直接匹配部分,而Xpath对于class跟普通属性一致,使用字符串精确匹配,须要使用contains()函数才能匹配部分字符串code

<div class="class1 popup js-dragable alert-msg">
    <div class ="class2 submit-box ">
            <input class ="class3"/>            
    </div>
</div> String locator_Xpath="//div[@class='class1 popup js-dragable alert-msg']//input[@class='class3']"; String locator_Xpath="//div[contains(@class,'popup js-dragable alert-msg')]//input[@class='class3']"; String locator_Css = ".class1.js-dragable .class3"

 

 4. 使用祖先元素属性与当前元素属性组合处理时blog

<div class="111">
    <div>
        <div>
            <input class = "222"/>
        </div>
    </div>
    
</div> String locator_xpath="//div[@class='111'/*/*/*[@class='222']]";
String locator_xpath="//div[@class='111'//[@class='222']]" String locator_css = ".111 .222";

 *注意两个class之间有一个空格表示层级关系,且空格选择全部层级的子元素,而>只选择一代字符串

 

5.模糊匹配input

  XPath Css
选取属性值中的部分string匹配 //span[contains(@class,'popup-btn js-dragable')] span[title*='456']
  //input[starts-with(@name,'name1')] input[name^='name1']
  //input[ends-with(@name,'name1')] input[name$='name1']
     

 

 

 

 

 

 **ends-with() function is part of XPath 2.0 but browsers generally only support 1.0.selenium

 

6.多个属性匹配string

    xpath=//a[@class='name' and value='123']产品

    css = a[.name][value='123']

 

7. 第n个子元素的选择

<div class="category_depth_1 bnr">
    <li><a href="/estore/kr/zh/c/1" class="on">护肤</a></li>
    <li><a href="/estore/kr/zh/c/1" class="on">家电</a></li>
    <li><a href="/estore/kr/zh/c/1" class="on">美妆</a></li>
    <li><a href="/estore/kr/zh/c/1" class="on">母婴</a></li>
    <li><a href="/estore/kr/zh/c/1" class="on">电子产品</a></li>
</div> String locator_Xpath = "//*[@class="category_depth_1 bnr"]//li[1]" String locator_Css = ".category_depth_1.bnr li:first-child" String locator_Css = ".category_depth_1.bnr li:last-child" String locator_Css = ".category_depth_1.bnr li:nth-child(1)" #index都是从1开始
以上元素若是选择点击li元素有可能点击不生效而选择点击a标签,这个时候须要注意index仍是要标在li标签后面 String locator_Xpath = "//*[@class="category_depth_1 bnr"]//li[1]/a" String locator_Css = ".category_depth_1.bnr li:first-child a" String locator_Css = ".category_depth_1.bnr li:last-child>a" String locator_Css = ".category_depth_1.bnr li:nth-child(1)>a"

 

8. 拥有某个属性的元素

  xpath= //*[@title]

  css= *[title]

9. 拥有子元素a的P元素

  xpath= //div[a]

  css 不能实现

10. 匹配祖先元素

  xpath= div[@id="id1"]//ancestor::tr//td

  css 不能实现

 11. 查找兄弟元素, Css只能查找元素后面的元素,不能向前找

  xpath= //div[@class="class1"]//preceding-sibling::div[1]

  xpath= //div[@class="class1"]//follow-sibling::div[1]

  css= div.class1+div

12. 查找不包含not,以不包含display: none为例

  xpath= //div[@class="name" and not(contains(@style,"display: none"))]

  css= div.name:not([style*='display: none'])

相关文章
相关标签/搜索