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")
相关推荐
程序员小远11 天前
自动化测试基础知识总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员三藏11 天前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
深蓝电商API11 天前
Selenium 5.0 全新架构解析:值得升级吗?
爬虫·selenium
小白学大数据14 天前
线上故障急救:依托 OpenClaw 日志排查 403 和 503 问题
爬虫·python·selenium·数据分析
糖果店的幽灵15 天前
AI 驱动 Selenium 测试框架最佳实践:从传统自动化到智能体测试
人工智能·selenium·自动化
Maydaycxc15 天前
Python 实现 RPA + AI 自动化:大模型 OCR + 网页操作完整源码实战
人工智能·python·opencv·selenium·自动化·ocr·rpa
赵大大宝15 天前
Selenium 从入门到精通:自动化测试与爬虫实战全攻略
爬虫·selenium·测试工具
深蓝电商API18 天前
Playwright vs Puppeteer vs Selenium 2026终极对比
爬虫·selenium·puppeteer·playwright
ClouGence20 天前
不用写 Selenium,零代码的 UI 自动化测试工具!
selenium·测试
@zulnger22 天前
selenium 操作浏览器
前端·javascript·selenium