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")
相关推荐
_OP_CHEN3 小时前
【测试理论与实践】(九)Selenium 自动化测试常用函数全攻略:从元素定位到文件上传,覆盖 99% 实战场景
自动化测试·python·测试开发·selenium·测试工具·测试工程师·自动化工具
智航GIS1 天前
10.4 Selenium:Web 自动化测试框架
前端·python·selenium·测试工具
廖圣平2 天前
从零开始,福袋直播间脚本研究【三】《多进程执行selenium》
python·selenium·测试工具
0思必得02 天前
[Web自动化] Selenium基础介绍
前端·python·selenium·自动化·web自动化
测试19982 天前
Web自动化测试入门
自动化测试·软件测试·python·功能测试·selenium·测试工具·测试用例
0思必得02 天前
[Web自动化] Selenium简单使用
前端·python·selenium·自动化·web自动化
小白学大数据3 天前
使用 Selenium 爬取京东手机销量与评分数据 (1)
爬虫·selenium·测试工具·智能手机
天才测试猿4 天前
软件测试之bug分析定位技巧
软件测试·python·selenium·测试工具·职场和发展·测试用例·bug
爆更小哇5 天前
Selenium自动化测试函数全解析(二)
java·selenium·测试工具·自动化