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")
相关推荐
2601_962295338 天前
如何python实现网页的自动化
python·selenium·beautifulsoup·requests·网页自动化
夜雪一千8 天前
XPath ancestor 轴详解:向上查找祖先节点,爬虫高频向上检索利器
xpath
程序员小远8 天前
自动化测试用例编写实例详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
2601_962298279 天前
Python与Selenium结合的Web自动化测试全流程实践教程
自动化测试·python·selenium·web测试·pageobjectmodel
2601_962298279 天前
【自动化测试】基于Selenium + Python的web自动化框架
自动化测试·python·selenium·测试用例·web框架
2601_9622955810 天前
python+selenium实现自动化测试
自动化测试·python·selenium·学习心得·web端测试
B2_Proxy11 天前
Selenium配置代理IP的完整指南:从启动参数到认证扩展
selenium·测试工具
weixin_4407305011 天前
Playwright介绍、特点、小例子还有与传统selenium的区别
selenium·测试工具·playwright
2601_9623878213 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest