python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > UI自动化定位

UI自动化定位常用实现方法代码示例

作者:铁扇公主

这篇文章主要介绍了UI自动化定位常用实现方法代码示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

8大基础定位

xpath定位

  //*[]:// 相对定位 * 匹配任意标签

  第一种:id\class\name\其他属性,精确匹配

driver.find_element_by_xpath("//*[@id='']") # id与id的值
driver.find_element_by_xpath("//*[@class='']") # class和class的值<br data-filtered="filtered">driver.find_element_by_xpath("//*[@name='']")     # naem和值<br data-filtered="filtered">driver.find_element_by_xpath("//*[@shuxingming='']") # 属性名和值

  第二种:模糊匹配\层级\索引\逻辑运算

  模糊匹配:

driver.find_element_by_xpath("//*[contains(text(),'测试')]")     # 包含某些字符
driver.find_element_by_xpath("//*[starts-with(text(),'测试')]")   # 以某些字符开头
driver.find_element_by_xpath("//*[ends-with(text(),'测试')]")     # 以某些字符结尾
driver.find_element_by_xpath("//*[matchs(text(),'测试')]")      # 正则匹配

  层级:

driver.find_element_by_xpath("//*[@id='']/p")

  索引:

driver.find_element_by_xpath("//*[@id='']/option[0]")

  第三种:绝对定位

  html/body/heard/div/divdiv/ul/li[2]/a 不推荐

css定位

  第一种:id\class\标签名

#:id

.:class

driver.find_element_by_css_selector("#username") #id为username
driver.find_element_by_css_selector(".username") #class为username
driver.find_element_by_css_selector("iframe") #标签名为iframe

 第二种:

索引:

driver.find_element_by_css_selector("selet#nr>option:nth-child(1)") #标签名:nth-child(1)来定位子元素

层级:

driver.find_element_by_css_selector("selet#nr>option") #标签名:nth-child(1)来定位子元素

逻辑运算:

driver.find_element_by_css_selector("input#nr[id=''][class='']") #不用and连接,写在一起即可

定位多组元素

使用 find_elements ,结果为列表,使用下标索引方式取值

names=driver.find_elements_by_name("username")
print names[1]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文