# 跳转到最新窗口
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")
selenium定位详解
qq_15729022052023-11-05 17:58
相关推荐
天才测试猿2 天前
如何封装自动化测试框架?ClouGence3 天前
8 款好用的 Web 自动化测试工具大盘点:怎么选、适合谁?城中南小6 天前
人机Agent团队协同:从Managed Agents原理到Multica实践ClouGence8 天前
零代码自动化测试工具回演 CueCast 新版本发布:数据变量、多标签页录制与登录准备能力上线标致的自行车12 天前
Selenium 爬虫固定开头:安然无虞12 天前
Python自动化测试·Selenium操控元素的方法Data_Journal12 天前
使用 Selenium 进行网页抓取:分步指南ClouGence16 天前
Selenium、Playwright、CueCast 深度对比:Web 自动化测试工具怎么选程序员小远1 个月前
自动化测试基础知识总结程序员三藏1 个月前
Web自动化测试详解