zoukankan      html  css  js  c++  java
  • selenium By.xpath 用法

    搞了一下午。。。

    1.     Xpath往往以“//”开头,属性都是采用@表示,例如//div[@id='_navigation']/div

    2.     Firefox有个插件叫做xpather,在页面上点击右键选中“show in xpather”,可以很快的给出对应的xpath。它给出的格式是这样的:/html/body/header/nav/ul[1]/li[5]/a/span

    3.     根据是否使用属性,Xpath的语法格式有两种:

    不使用属性:按照html的层次,如/html/body/header/nav/ul[1]/li[5]/a/span

    使用属性:如//input[@class=’input’],直接定位。

    Selenium还支持多个属性来定位,如//input[@class=’input’ and @type=’text’]。

    4.     /div[2]表示第二个,/div[last()]表示最后一个,但是没有/div[first()]的语法,选择第一个用/div[1]

    5.     选择一个以上的元素,使用|,如//div|//a,表示选择所有的div标签和a标签

    6.     选择未知元素,使用“*”来选择满足条件的所有元素,如/*

    7.     //input[@class=’input’]选择元素中的属性

           //input[@class]选择有属性名为class的input标签

           //input[@]选择有属性的input标签

    点击页面上的File菜单, 用selenium怎么抓多层DIV里的路径:

     代码大概形式   div嵌套了10层
    <div id="isc_2M">
          ...(中间省略8个div)
             <div id="ISC_2O">
                 <table>
                   <tbody>
                        <tr>
    <td>
                              <table>
                                 <tbody>
                                    <tr>
                                       <td>File
                                       </td>
                                    </tr>
                                 </tbody>
                              </table>
                           </td>
                        </tr>
                   </tbody>
                 <table>
             </div>
          ...
    </div>

    需要点击<td>File</td>这个

    这个:
    1 selenium.click("//td[text()='File']");
    2 selenium.click("//td[contains(text(),‘File’)]");
    3 selenium.click("//div/table/tbody/tr[1]/td[1]/table/tbody/tr[1]/td[1]");

    4 selenium.click("//div[@id='ISC_2O']/table//table//tr[1]/td");

    相关博客

    http://blog.sina.com.cn/s/blog_505bf9af0101bdtq.html
    http://www.51testing.com/html/91/79191-829759.html
    http://www.cnblogs.com/qingchunjun/p/4208159.html(重要博客)
  • 相关阅读:
    Ubuntu 16.04
    每天一道LeetCode--389. Find the Difference
    每天一道LeetCode--371. Sum of Two Integers
    Ubuntu 16.04 小飞机启动失败
    每天一道LeetCode--344. Reverse String
    leetcode1458 Max Dot Product of Two Subsequences
    CF1313C2 Skyscrapers (hard version)
    CF1295C Obtain The String
    CF1251D Salary Changing
    CF1286A Garland
  • 原文地址:https://www.cnblogs.com/tsw123/p/4836361.html
Copyright © 2011-2022 走看看