自动化测试定位汇总和 CSS--xpath定位的区别


1.

8种定位方式汇总

定位方式 说明 举例
id 根据元素属性来定位
name 根据元素属性来定位
class_name 根据元素属性来定位
tag_name 根据元素标签名称来定位
link_text 定位a标签(超链接)全部匹配
partial_link_text 定位a标签(超链接)模糊匹配
xpath

1.路径定位

        绝对路径用/开头

        相对路径用//开头

2.属性

           //input[@id="name"]

3.属性和层级结合

           //p[@id="name"]/input   p标签下的input标签

4. 属性和逻辑相结合

           //p[@id="name" and class="name"]

css

1. id    #id 

2.class   .class

3. 元素   element

4. 属性   [attribute=value] 

5.层级  element>element

           >可以使用空格代替

          子元素可以跟属性配合定位

       

1. #user

2. .Tel

3.  input

4.  [id='user']

5. p>input

        p input   (>空格代替)

        p>input[id="user"]

2.

css和xpath定位的区别

定位方式 xpath css
id //input[@id='user'] #user
class //input[@class='user'] .user
元素名 //input input
属性

1. //*[text()="文本内容"]

2. //*[starts-with(@attribute, "xxx")]

3. //*[contains(@attribute, "xxx")]

1. input[type^='a']

2. input[type$='b']

3. input[type*='c']

3.

  另外一种定位延伸:  By类定位方法

      说明: 统一调用find_element()方法,   通过By来声明定位的方法,并传入对应的方法和参数
               需要提供的两个参数 第一个参数定位的类型由By提供, 第二个参数为定位的具体方式


       使用:

               第一步 导包
                from selenium.WebDriver.common.by import By
              第二步 定位  

   # 对应8种定位方法
    1. driver.find_element(By.ID,"user").send_keys("admin")
    
    2. driver.find_element(By.NAME,"password").send_keys("123456")

    3. driver.find_element(By.CLASS_NAME,"tel").send_keys("15999999999")

    4. driver.find_element(By.TAG_NAME,'input').send_keys("cc")

    5. driver.find_element(By.LINK_TEXT,'百度').click()

    6. driver.find_element(By.PARTIAL_LINK_TEXT,'百度一下').click()

    7. driver.find_element(By.CSS_SELECTOR,'#user').send_keys("zhangsan")

    8. driver.find_element(By.XPATH,'//*[@id="email"]').send_keys('[email protected]')
发布了106 篇原创文章 · 获赞 11 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/object_oriented_/article/details/100066482