当前位置:网站首页>Element positioning in selenium is correct, but the operation fails. Six solutions are all finalized
Element positioning in selenium is correct, but the operation fails. Six solutions are all finalized
2022-07-23 19:37:00 【Software testing】
selenium The reasons for the correct positioning of the element in but the failure of the operation are as follows 4 Kind of :
One 、 The page is not loaded well
resolvent : Add a wait method , Such as : time.sleep()
Two 、 Page submission needs to wait for the data background
resolvent : Add a wait method , Such as : time.sleep()
3、 ... and 、 The browser is not opened in a maximized way , Causes the element to be hidden , Can't locate
resolvent :
Maximize the web page :driver.maximize_window()
If the screen scale is fixed , Limited resolution , If there is no way to maximize the web page , Can be executed by JS Method to implement .
browser.execute_script(‘window.scrollBy(200,0)’) # here (200,0) Offset coordinates for web pages ,200 Abscissa ,0 Vertical coordinates
#window.scrollBy() by JS Method #
Four 、 There are multiple in the web page frame frame , The positioned element is not in the current frame in . So although the element clearly seems to be positioned correctly , But clicking doesn't work .
frame Frame function :frame Object represents a HTML frame ,frame Is a sub window , You can load web pages inside , When the internal sub window is refreshed , Will not affect the external window .
For nested multiple frame The page of , You need to jump layer by layer during operation , Jump to the element frame.
frame The label has frameset、frame、iframe Three ,frameset Like other ordinary labels , It will not affect the positioning of elements ; and frame And iframe The switching method is the same .
frame Next is an independent HTML file :
resolvent : You can use selenium Medium switch_to.frame() Method to implement frame Jump between .
The specific operation is as follows :
1. How to frame Switch ?
utilize switch_to.frame() Method to switch .
switch_to.frame(reference)
reference Is the parameter passed in , Used to locate frame, It can be id,name,tag name etc. . Let's say there's the following HTML Code :
<html lang="en">
<head>
<title>FrameTest</title>
</head>
<body>
<iframe src="myframe.html" id="frame1" name="myframe"></iframe>
</body>
</html>
You want to switch to the above iframe The way to do this is :
driver.switch_to.frame(0) # use frame Of index location , The first is 0
driver.switch_to.frame("frame1") # use frame Of id To locate
driver.switch_to.frame("myframe") # use name To locate
driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # use iframe Corresponding tag name To locate
driver.switch_to_frame(driver.find_element_by_xpath("//iframe[contains(@src,'myframe')]")) # utilize xpath To locate
2. How to Congzi frame Switch back to the main document ?
Switch to child frame After that, you can no longer operate on the main document elements , So switch back to the main document . Method is :driver.switch_to.default_content()
driver.switch_to.default_content()
3. Multi-storey frame How to switch ?
If there are multiple layers frame Switch layer by layer , For example, the following multiple frame Nested HTML Code :
<html>
<iframe id="frame1">
<iframe id="frame2" / >
</iframe>
</html>
(1) Switch from the main document to frame 2:
driver.switch_to.frame(‘frame1’) # First switch from the main document to frame1
driver.switch_to.frame(‘frame2’) # Again from frame1 Switch to frame2
(2) from frame2 Cut back to frame1:
from frame2 Retrace frame1, here selenium It provides us with a way to learn from children frame Cut back to the father frame, Instead of cutting back to the main document and then cutting in .
driver.switch_to.parent_frame() # If it is already the main document , No effect
With parent_frame() This is equivalent to the method of retreating , We can switch between different frame, Jump around casually .
Sum up , encounter frame Jump problem , Use the following three methods to solve :
driver.switch_to.frame(reference)
driver.switch_to.parent_frame()
driver.switch_to.default_content()
5、 ... and 、 Element attributes are dynamic attributes , Look at the correct positioning , But as the mouse clicks on different positions , Attribute of element changes .
For example, the contents of the following page class The attribute changes as the element is clicked :
resolvent : Change another fixed attribute of the element to locate .
6、 ... and 、 Page elements are blocked by other pop-up windows when the page is opened .
resolvent : Add step of closing pop-up window .
The house needs to be built layer by layer , Knowledge needs to be learned at one point one . We should lay a good foundation in the process of learning , More hands-on practice , Don't talk much , The last dry goods here ! I stayed up late to sort out the stages ( function 、 Interface 、 automation 、 performance 、 Test open ) Skills learning materials + Practical explanation , Very suitable for studying in private , It's much more efficient than self-study , Share with you .
Get off w/x/g/z/h: Software testing tips dao
Typing is not easy , If this article is helpful to you , Click a like, collect a hide and pay attention , Give the author an encouragement . It's also convenient for you to find it quickly next time .
边栏推荐
- AtCoder Regular Contest 144【VP记录】
- .net core implements background tasks (scheduled tasks) longbow Tasks component (III)
- 记一次被网络诈骗的经历
- What content does the software test plan include and how to write it. Share test plan template
- redis过期key的删除策略[通俗易懂]
- H7-TOOL的CANFD/CAN接口脱机烧写操作说明, 已经更新(2022-07-12)
- 微信小程序自己实现一个全局事件总线
- PowerCLi 管理VMware vCenter 批量部署导出导入
- USB3.0:VL817Q7-C0的LAYOUT指南
- BoundsChecker用法「建议收藏」
猜你喜欢

LeetCode每日一题(1514. Path with Maximum Probability)

Elk note 25 - expérience rapide APM
![[C language] program environment and preprocessing](/img/5c/7f14c73e075a54a11d7ff44907c30d.jpg)
[C language] program environment and preprocessing

看完这篇,彻底搞懂 gRPC!

UPC 2022 summer personal training game 12 (number of combinations b)

【C语言】程序环境和预处理

Challenges of decentralized storage

Data link layer -------- Ethernet and ARP
![二叉树高度 [log2n]+1与log2(n+1)是否相等](/img/64/381376190218d5b2cdfd8b1197e8f6.png)
二叉树高度 [log2n]+1与log2(n+1)是否相等

Type-C Bluetooth speaker single C-Port rechargeable OTG solution
随机推荐
H7-TOOL的CANFD/CAN接口脱机烧写操作说明, 已经更新(2022-07-12)
R language mapping: coordinate axis setting
Boundschecker usage "recommended collection"
【leetcode天梯】链表 · 022 链表中倒数第k个节点
BoundsChecker用法「建议收藏」
DP problem collection
Technical scheme of face recognition system
LeetCode每日一题(1514. Path with Maximum Probability)
R语言使用tidyr包的gather函数将从宽表转化为长表(宽表转化为长表)、第一个参数指定原多个数据列名称生成的新数据列名称、第二个参数指定原表内容值、第三个和第四个参数指定不变的列名称列表
How can mysqldump export content without comments?
Labyrinth DP integration
虹科干货 | 教您如何解析MODBUS中的浮点型数据
【开发经验】开发项目踩坑集合【持续更新】
[Nuxt 3] (九)服务器路由
SecureCRT乱码问题解决方法[通俗易懂]
R语言作图:坐标轴设置
A preliminary study of the relationship between combinatorial mathematics and DP, and the derivation of resettable combinatorial formulas
R语言使用ggpubr包的ggarrange函数将多幅图像组合起来、使用ggexport函数将可视化图像保存为bmp格式(width参数指定宽度、height参数指定高度、res参数指定分辨率)
总结一些最近见到的 TRICK
Figure learning summary