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")
相关推荐
工会主席-阿冰5 小时前
使用pytest-selenium插件,ui自动化示例
selenium·pytest
认真的小羽❅1 天前
Python Selenium 超详细新手教程:从零开始掌握浏览器自动化
selenium·测试工具·自动化
西游音月2 天前
(6)pytest+Selenium自动化测试-测试用例编写
selenium·测试用例·pytest
Wpa.wk2 天前
自动化测试 - 文件上传 和 弹窗处理
开发语言·javascript·自动化测试·经验分享·爬虫·python·selenium
mike04122 天前
Eclipse+maven+selenium自动化测试用例入门
selenium·eclipse·maven
啊巴矲3 天前
小白从零开始勇闯人工智能:爬虫初级篇(Selenium库)
爬虫·selenium·测试工具
尼罗河女娲3 天前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(一)
websocket·网络协议·selenium
尼罗河女娲3 天前
【测试开发】Selenium + Chrome 自动化中常用 ChromeOptions 参数说明(实践总结)
chrome·selenium·自动化
爱打代码的小林3 天前
python(爬虫selenium)
爬虫·python·selenium
尼罗河女娲3 天前
【获取WebSocket】使用 Playwright 监听 Selenium 自动化测试中的 WebSocket 消息(二)
websocket·网络协议·selenium