当前位置:网站首页>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"]')
边栏推荐
- RDS数据库的监测页面在哪看?
- Where is the database account used when running SQL tasks in data warehouse tasks configured
- [RT thread] NXP rt10xx device driver framework -- Audio construction and use
- Vs code plug-in korofileheader
- SSH连接远程主机等待时间过长的解决方法
- Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
- How do large consumer enterprises make digital transformation?
- Take you to API development by hand
- Leetcode13. Roman numeral to integer (three solutions)
- Kotlin学习快速入门(7)——扩展的妙用
猜你喜欢

Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
![[try to hack] active detection and concealment technology](/img/43/d48f851268fec566ce0cc83bd9557e.png)
[try to hack] active detection and concealment technology

Simple use of unity pen XR grab

Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires

大变局!全国房价,跌破万元大关

SWM32系列教程4-端口映射及串口应用

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

How to train mask r-cnn model with your own data

【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
![[UE4] brush Arctic pack high quality Arctic terrain pack](/img/e7/bc86bd8450b0b2bdec8980a2aa1a10.jpg)
[UE4] brush Arctic pack high quality Arctic terrain pack
随机推荐
C语言字符串练习
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
静态程序分析(一)—— 大纲思维导图与内容介绍
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
Kotlin learning quick start (7) -- wonderful use of expansion
Redis:关于列表List类型数据的操作命令
Life is still confused? Maybe these subscription numbers have the answers you need!
ANOVA example
New library online | cnopendata complete data of Chinese insurance institution outlets
Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
vs code 插件 koroFileHeader
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
PS screen printing brush 131, many illustrators have followed suit
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
建立自己的网站(23)
绝对定位时元素水平垂直居中