当前位置:网站首页>WEB-UI自动化测试-最全元素定位方法
WEB-UI自动化测试-最全元素定位方法
2022-07-03 17:21:00 【不要问我y】
# BY.ID
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
driver.find_element(By.ID, "kw").send_keys("自动化")
# BY.NAME
driver.find_element(By.NAME, "wd").send_keys("自动化")
# BY.LINK_TEXT,链接文本
driver.find_element(By.LINK_TEXT, "新闻").click()
# BY.PARTIAL_LINK_TEXT,部分文本链接
driver.find_element(By.PARTIAL_LINK_TEXT, "新").click()
# BY.XPATH定位,分为五种
# 相对路径+索引定位
# 定位form模块第一个span标签下的input
driver.find_element(By.XPATH, "//form/span[1]/input")
# 相对路径+属性定位
driver.find_element(By.XPATH, "//[@autocomplete = 'off']")
# 相对路径+通配符定位,*是通配符,匹配所有属性值为off的属性
driver.find_element(By.XPATH, "//*[@* = 'off']")
driver.find_element(By.XPATH, "//*[@autocomplete = 'off']")
# 相对路径+部分属性值定位,属性要唯一
driver.find_element(By.XPATH, "//*[starts-with(@autocomplete.'ff')]")
driver.find_element(By.XPATH, "//*[substring(@autocomplete,2) = 'ff']")
driver.find_element(By.XPATH, "//*[contains(@autocomplete ,'ff')]")
# 相对路径+文本标签值定位
driver.find_element(By.XPATH, "//span[text() = '按图片搜索']")
# 组合定位
# 标签名#id属性值:指的是该input标签下id属性为kw的元素
driver.find_element_by_css_selector("input#kw")
# 标签名.class属性值:指的是该input标签下class属性为s_ipt的元素
driver.find_element_by_css_selector("input.s_ipt")
# 标签名[属性=’属性值‘]:指的是该input标签下name属性为wd的元素
driver.find_element_by_css_selector('input[name="wd"]')
# 父级元素标签名>标签名.class属性值:指的是span下的input标签下class属性为s_ipt的元素
driver.find_element_by_css_selector("span>input.s_ipt")
# 多个属性组合定位元素(常用)
# 指的是input标签下id属性为kw且name属性为wd的元素
driver.find_element_by_css_selector('input.s_ipt[name="wd”]')
# 指的是input标签下name属性为wd且maxlength为100的元素
driver.find_element_by_css_selector('input[name="wd"][maxlength="100"]')
边栏推荐
- 图之深度优先搜索
- How do large consumer enterprises make digital transformation?
- Select 3 fcpx plug-ins. Come and see if you like them
- Comparison of kotlin collaboration + retro build network request schemes
- AcWing 4489. Longest subsequence
- Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
- RedHat 6.2 配置 Zabbix
- Installation and configuration of network hard disk NFS
- Design e-commerce spike
- Kotlin learning quick start (7) -- wonderful use of expansion
猜你喜欢

跨境电商:外贸企业做海外社媒营销的优势

Select 3 fcpx plug-ins. Come and see if you like them

kubernetes资源对象介绍及常用命令(四)

1164 Good in C

Golang unit test, mock test and benchmark test
![[RT thread] NXP rt10xx device driver framework -- RTC construction and use](/img/19/91a9d84ba84f81ef125c33eb4007bc.png)
[RT thread] NXP rt10xx device driver framework -- RTC construction and use

2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)

The largest matrix (H) in a brush 143 monotone stack 84 histogram

【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用

Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
随机推荐
AcWing 4489. 最长子序列
kubernetes资源对象介绍及常用命令(四)
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
大消费企业怎样做数字化转型?
【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
静态程序分析(一)—— 大纲思维导图与内容介绍
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
新库上线 | CnOpenData中国观鸟记录数据
Host based intrusion system IDS
TensorBoard快速入门(Pytorch使用TensorBoard)
RDS数据库的监测页面在哪看?
建立自己的网站(23)
Hongmeng third training
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
POM in idea XML graying solution
What is the difference between cloud server and cloud virtual machine
Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
线程池:业务代码最常用也最容易犯错的组件
定义一个结构体Fraction,表示分数,用于表示 2/3, 5/6这样的分数