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")
相关推荐
若惜19 小时前
selenium自动化测试web自动化测试 框架封装Pom
前端·python·selenium
念越2 天前
自动化测试入门指南:Selenium环境搭建+第一个实战案例
自动化测试·selenium·测试工具·蓝桥杯
小邓睡不饱耶2 天前
实战教程:基于Selenium+BeautifulSoup爬取易车网新能源汽车销量数据
selenium·测试工具·beautifulsoup
sheepfagdng2 天前
Python-web自动化-selenium(2)
运维·selenium·自动化
虚幻如影2 天前
Selenium 自动化测试中 Chrome 浏览器弹出“您的连接不是私密连接”
chrome·selenium·测试工具
sheepfagdng2 天前
python-web自动化-selenium(1)
selenium·测试工具
qq_427940343 天前
java-UI自动化selenium+TestNG
java·selenium·自动化
Looooking4 天前
Python 之自动下载更新 selenium 驱动 chromedriver
python·selenium·chromedriver
Wpa.wk5 天前
Selenium自动化 - headless无头浏览器配置 + Capability配置
selenium·测试工具·自动化
智算菩萨5 天前
深度解析ChatGPT 5.4赋能Python Selenium网页自动化测试:从理论到实践的完整指南
人工智能·python·selenium·ai·chatgpt