UI自动化定位常用实现方法代码示例
作者:铁扇公主 时间:2022-09-22 09:47:13
8大基础定位
driver.find_element_by_id() # id定位
driver.find_element_by_name() # name定位
driver.find_element_by_class_name() # class名称定位
driver.find_element_by_tag_name() # 标签定位
driver.find_element_by_xpath() # xpath定位
driver.find_element_by_css_selector() # css定位
driver.find_element_by_link_text() # a标签的文本定位
driver.find_element_by_partial_link_text() # a标签的局部文本定位
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]
来源:https://www.cnblogs.com/princessironfan/p/13667957.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
安装PHP可能遇到的问题“无法载入mysql扩展” 的解决方法
ASP获取刚插入记录的自动编号ID
CSS双线边框研究
![](https://img.aspxhome.com/file/UploadPic/20099/3/db_01-71s.jpg)
SQL Server取得网站路径的几种方法及比较
一文了解Django缓存机制
Python可视化学习之seaborn绘制矩阵图详解
![](https://img.aspxhome.com/file/2023/3/90883_0s.jpg)
python中的线程threading.Thread()使用详解
![](https://img.aspxhome.com/file/2023/5/97395_0s.png)
解读Opencv中Filter2D函数的补全方式
![](https://img.aspxhome.com/file/2023/5/98885_0s.png)
了解WEB页面工具语言XML(六)展望
四大因素扼杀了中国人的创造力
XHTML 1.0:标记新的开端
Python 中的装饰器实现函数的缓存(场景分析)
TensorFlow如何指定GPU训练模型
![](https://img.aspxhome.com/file/2023/8/82818_0s.png)
Python中暂存上传图片的方法
如何利用Python实现一个论文降重工具
![](https://img.aspxhome.com/file/2023/3/108133_0s.png)
python超详细实现完整学生成绩管理系统
![](https://img.aspxhome.com/file/2023/5/82775_0s.png)