selenium定位详解

复制代码
# 跳转到最新窗口
driver.switch_to.window(driver.window_handles[-1])

# 跳转到iframe,属性id,name,class均可
driver.switch_to.frame("passport_iframe")

# 根据class属性定位
driver.find_element(By.CLASS_NAME, "toolbar-btn-loginfun").click()

# 发布文章,根据带空格的class属性定位
driver.find_element(By.CSS_SELECTOR, ".button.btn-b-red.ml16").click()

# 根据文本内容定位,点击发布按钮
driver.find_element(By.LINK_TEXT, "发布").click()

#查找任意标签下,class=xxx的元素
driver.find_element(By.XPATH, "//*[@class='xxx']")

查找button标签下,class=xxx的元素
driver.find_element(By.XPATH, "//button[@class='xxx']")

查找button标签下,文本内容为"发布文章"并且class=xxx的元素
driver.find_element(By.XPATH, "//button[text()='发布文章' and class='xxx' ]")

查找button标签下,文本内容为"发布文章"并且class内包含xxx关键字的元素
driver.find_element(By.XPATH, "//button[text()='发布文章' and contains(@class,'xxx') ]")

查找button标签下,class属性中开始位置为'abcd'关键字的元素
driver.find_element(By.XPATH, "//button[starts-with@class,'abcd')]")

查找button标签下,class属性中结尾位置为'xyz'关键字的元素
driver.find_element(By.XPATH, "//button[end-with@class,'xyz' ]")


定位最后一个元素
driver.find_element(By.XPATH, "//button[last()]")
定位倒数第二个
driver.find_element(By.XPATH, "//button[last()-1]")

定位第一个text()="发布文章"
driver.find_element(By.XPATH, "//button[text()='发布文章' and position()=1 ]")

CSS_SELECTOR定位class带空格的元素
driver.find_element(By.CSS_SELECTOR, ".button.btn-b-red.ml16")
相关推荐
憧憬成为原神糕手2 天前
Playwright、Selenium、DrissionPage 与 JSON 接口怎么选?
数据库·selenium·json
天才测试猿6 天前
实例介绍:Unittest框架及自动化测试实现流程
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员三藏8 天前
自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
独行侠影a8 天前
Selenium 爬虫反爬实战:Python 模拟浏览器点击动态页面采集
爬虫·python·selenium
祉猷并茂,雯华若锦10 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦12 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest
黑客-秋凌13 天前
使用Python+selenium实现第一个自动化测试脚本
开发语言·自动化测试·软件测试·python·selenium·测试工具
祉猷并茂,雯华若锦14 天前
Win下完美解决Allure报错,生成Web自动化测试报告
android·python·selenium·自动化
测试修炼手册14 天前
[测试技术] Selenium WebDriver 入门与实战:稳定等待、Page Object 与失败排障
selenium·测试工具