当前位置:网站首页>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"]')
边栏推荐
- 27. Input 3 integers and output them in descending order. Pointer method is required.
- 1164 Good in C
- 免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
- AcWing 4489. Longest subsequence
- kubernetes资源对象介绍及常用命令(三)
- Squid service startup script
- How to train mask r-cnn model with your own data
- Select 3 fcpx plug-ins. Come and see if you like them
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
猜你喜欢
Talk about several methods of interface optimization
Great changes! National housing prices fell below the 10000 yuan mark
QT adjust win screen brightness and sound size
C language modifies files by line
鸿蒙第三次培训
vs2013已阻止安装程序,需安装IE10
图之深度优先搜索
Leetcode: lucky number in matrix
Qt调节Win屏幕亮度和声音大小
【Try to Hack】主动侦查隐藏技术
随机推荐
[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
Swm32 series Tutorial 4 port mapping and serial port application
大变局!全国房价,跌破万元大关
Vs code plug-in korofileheader
vs2013已阻止安装程序,需安装IE10
Luogu: p2685 [tjoi2012] Bridge
[try to hack] active detection and concealment technology
【JokerのZYNQ7020】DDS_ Compiler。
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
What is the difference between cloud server and cloud virtual machine
AcWing 4489. 最长子序列
C语言按行修改文件
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
【Try to Hack】主动侦查隐藏技术
How to train mask r-cnn model with your own data
Simple use of unity pen XR grab
RedHat 6.2 配置 Zabbix
Static program analysis (I) -- Outline mind map and content introduction
问题随记 —— 在 edge 上看视频会绿屏
Kotlin learning quick start (7) -- wonderful use of expansion