当前位置:网站首页>自动化测试定位不到元素?可能是 frame 在搞鬼
自动化测试定位不到元素?可能是 frame 在搞鬼
2022-08-06 08:01:00 【程序员威子】
很多人在用Splinter或Selenium定位页面元素的时候会遇到定位不到的问题,明明元素就在那儿,就是定位不到,这种情况很有可能是frame在搞鬼。
说白了就是网站上的网页A,又嵌入了其他网页B。你访问了网页A,在里面可以看到网页B的元素,但使用工具就是无法操作B,笨方法就是找到B的链接,去访问B,再来操控B上的元素。
聪明的方法就是使用Selenium提供的 switch_to.frame() 方法来切换frame。
切换 frame 的方法
假如有这样的网页:
<html>
<head>
<title>FrameTest</title>
</head>
<body>
<iframe src="a.html" id="frame1" name="myframe">
</iframe>
</body>
</html>
复制代码可以使用如下的方法:
from selenium import webdriver
driver = webdriver.Firefox()
driver.switch_to.frame(0) # 1.用frame的index来定位,第一个是0
或使用 id 来定位:
driver.switch_to.frame("frame1") # 2.用id来定位
或用 name 来定位:
driver.switch_to.frame("myframe") # 3.用name来定位
或使用 WebElement对象来定位:
#driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # 4.用WebElement对象来定位
如果是 splinter,则可以这样:
mybrowser.driver.switch_to.frame(0)
切回主文档的方法
切到 frame 中之后,我们便不能继续操作主文档的元素,这时如果想操作主文档内容,则需切回主文档。
driver.switch_to.default_content()
有时候我们会遇到嵌套的 frame,如下:
<html>
<iframe id="frame1">
<iframe id="frame2" / >
</iframe>
</html>
复制代码driver.switch_to.frame("frame1")
driver.switch_to.frame("frame2")
则可以使用以下方式切回父 frame:
driver.switch_to.parent_frame() # 如果当前已是主文档,则无效果
总结:
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取
边栏推荐
猜你喜欢

DemographicTable 新的基线特征表绘制 R包

C language force buckle the 59th spiral matrix ②.analog matrix

Typescript variable

CSDN official plug-in

卡片层叠Banner

自用工具 猴子都会用的unity视频播放器

CSDN官方插件

dalle2:hierarchical text-conditional image generation with clip

How to ensure the security of NFT from the suspected abolition of the magic core

Why do interviewers keep asking technical questions on your resume until they can't answer them?
随机推荐
Parameter ‘courseId‘ not found. Available parameters are [arg1, arg0, param1, para
EsgynDB Troubleshooting - 网卡MTU导致跨网段访问数据库失败
2022海亮SC游记
【Redhat】新系统yum源配置
测试用例设计方法-场景法详解
UNIX环境高级编程-第二章
“数字化重构系统, CEO 是第一步”
OpenAtom XuperChain 开源双周报 |2022.7.25-2022.8.5
C语言 结构体
Rock vocal | completes the safety operation, is not so difficult as you think
猴子都能上手的PUN扩展(房间密码,房间列表,RTC)
禁止运行游戏的程序开发
【matlab的积分,傅里叶,拉普拉斯变换,信号分析】
2022 Hailiang SC Travel Notes
ggplot2绘图进阶:如何在不同分面添加不同图形
七夕玫瑰花合集
Datax3.0+DataX-Web打造分布式可视化ETL系统
代码签名证书多少钱?
how to jump higher
Autumn begins tomorrow, the weather is getting cooler