当前位置:网站首页>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"]')
边栏推荐
- Great changes! National housing prices fell below the 10000 yuan mark
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
- 建立自己的网站(23)
- [combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
- Hongmeng third training
- Leetcode: lucky number in matrix
- Assembly instance analysis -- screen display in real mode
- [combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
- Hongmeng fourth training
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
猜你喜欢

New library online | cnopendata complete data of Chinese insurance institution outlets

UE4 official charging resources, with a total price of several thousand

Simple use of unity pen XR grab

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

ANOVA example

vs2013已阻止安装程序,需安装IE10

Qt调节Win屏幕亮度和声音大小
![Luogu: p2685 [tjoi2012] Bridge](/img/f5/f77027288a211ae466781b09ce650f.jpg)
Luogu: p2685 [tjoi2012] Bridge

大消费企业怎样做数字化转型?

Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
随机推荐
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
Installation and configuration of network hard disk NFS
[UE4] brush Arctic pack high quality Arctic terrain pack
人生还在迷茫?也许这些订阅号里有你需要的答案!
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
鸿蒙第四次培训
線程池:業務代碼最常用也最容易犯錯的組件
Applet setting multi account debugging
[RT thread] NXP rt10xx device driver framework -- pin construction and use
问题随记 —— 在 edge 上看视频会绿屏
C语言按行修改文件
Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
Rsync远程同步
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
SWM32系列教程4-端口映射及串口应用
【Try to Hack】主动侦查隐藏技术
跨境电商:外贸企业做海外社媒营销的优势
QT adjust win screen brightness and sound size
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6