selenium4 元素定位

selenium4 9种元素定位

ID

python 复制代码
driver.find_element(By.ID,"kw")

NAME

python 复制代码
driver.find_element(By.NAME,"tj_settingicon")

CLASS_NAME

python 复制代码
driver.find_element(By.CLASS_NAME,"ipt_rec")

TAG_NAME

python 复制代码
driver.find_element(By.TAG_NAME,"area")
python 复制代码
driver.find_element(By.LINK_TEXT,"hao123")
python 复制代码
driver.find_element(By.PARTIAL_LINK_TEXT,"hao")

XPATH

1、 普通语法
2、轴定位

关系:要找的元素是已知元素的XXX关系

用法: //...已知元素/轴定位名称::标签名@xx=xx

①parent:: 表示选取当前节点的父节点。 与 ... (两个点)的作用是一样的。

python 复制代码
driver.find_element_by_xpath('//a[@id="title6"]/parent::span/parent::form[@id="form"]')

②ancestor:: 表示当前节点的直系先辈(父、祖父等)元素,不包含叔叔伯伯。

python 复制代码
driver.find_element_by_xpath('//a[@id="hongloumeng"]/br/ancestor::form')

③descendant:: 表示选取当前节点的所有的 直系 后代(孙子、儿子、孙子的儿子等)元素。

python 复制代码
#通过元素定位表达式找到其中的一个a标签
driver.find_element_by_xpath('//form[@id="form"]/descendant::a[@id="title6"]')
#通过索引找到了所有的a标签中的第一个a标签
driver.find_element_by_xpath('//form[@id="form"]/descendant::a[1]') 

④preceding:: 表示选取当前节点的开始标签之前的所有的节点。

python 复制代码
driver.find_element_by_xpath('//input[@value="123"]/preceding::a[3]')

⑤preceding-sibling:: sibling表示兄弟姐妹的意思。表示选取当前节点开始标签之前的所有的同级元素。

python 复制代码
driver.find_element_by_xpath('//input[@value="123"]/preceding-sibling::a[1]')

⑥following:: 表示选取当前节点的结束标签之后的所有的元素。

python 复制代码
driver.find_element_by_xpath('//input[@value="123"]/following::span[1]')

⑦following-sibling:: 表示选取当前节点结束标签之后的 所有的同级标签。

python 复制代码
driver.find_element_by_xpath('//input[@value="123"]/following-sibling::span')

例子:

①定位到课程人数(通过兄弟找兄弟)

//ptext()='课程人数'/preceding-sibling::p

②定位到"柠檬班倒是ice"行的"私信"

//spantext()="柠檬班导师ice"/ancestor::tr//spantext()="私信"

或者

//spantext()="柠檬班导师ice"/ancestor:td/following-sibling:td//spantext0="私信"

CSS_SELECTOR

python 复制代码
#通过id定位  #表示id
driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("1111")
#通过class定位   .表示class
driver.find_element(By.CSS_SELECTOR,".ipt_rec").click()
#通过标签定位和id组合定位
driver.find_element(By.CSS_SELECTOR,"input#kw").send_keys("ccc")

#通过层级关系定位 > 表示上下级
driver.find_element(By.CSS_SELECTOR,"form#form>span>input#kw").send_keys("css")

#其他属性定位 需要加[]
# <input id="kw" class="s_ipt" autocomplete="off" maxlength="255" value="" name="wd"/>
driver.find_element(By.CSS_SELECTOR,"[autocomplete='off']").send_keys("xxx")
driver.find_element(By.CSS_SELECTOR,"[maxlength='255']").send_keys("xxx")
driver.find_element(By.CSS_SELECTOR,"[name='wd']").send_keys("xxx")

#css也可以通过索引nth-child(1)来定位子元素,直接翻译过来就是第几个小孩
driver.find_element(By.CSS_SELECTOR,".mnav:nth-child(2)").click()

# nth-of-type(3)
driver.find_element(By.CSS_SELECTOR,"a.mnav:nth-of-type(3)").click()

#同时满足两个属性
driver.find_element(By.CSS_SELECTOR,"[autocomplete='off'[name='wd']").send_keys("Xxx")
网格定位

above、below、to left of、to right of、near

driver.find_element(locate_with(By.TAG_NAME,"input").above({By.XPATH:"//@ID=""pwd"}))

案例:定位到登录按钮

python 复制代码
driver.find_element(locate_with(By.TAG_NAME,"div").to_right_of({By.XPATH:'//div[text()="帮助中心"]'})).click()
相关推荐
憧憬成为原神糕手1 天前
Playwright、Selenium、DrissionPage 与 JSON 接口怎么选?
数据库·selenium·json
天才测试猿5 天前
实例介绍:Unittest框架及自动化测试实现流程
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员三藏7 天前
自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
独行侠影a7 天前
Selenium 爬虫反爬实战:Python 模拟浏览器点击动态页面采集
爬虫·python·selenium
祉猷并茂,雯华若锦9 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦11 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest
黑客-秋凌12 天前
使用Python+selenium实现第一个自动化测试脚本
开发语言·自动化测试·软件测试·python·selenium·测试工具
祉猷并茂,雯华若锦13 天前
Win下完美解决Allure报错,生成Web自动化测试报告
android·python·selenium·自动化
测试修炼手册13 天前
[测试技术] Selenium WebDriver 入门与实战:稳定等待、Page Object 与失败排障
selenium·测试工具