当前位置:网站首页>Learn selenium to simulate mouse operation, and you can be lazy a little bit
Learn selenium to simulate mouse operation, and you can be lazy a little bit
2022-07-01 16:03:00 【TEST_ Two black】
Preface
We're doing it Web When automating , Sometimes the elements of the page don't need us to click , Value needs to move the mouse to display all kinds of information .
At this time, we can operate the mouse to achieve , Next, let's talk about using selenium do Web How to operate the mouse during Automation .
The mouse operation , We can use selenium Of ActionChains Class to achieve , Let's get to know this class first .
01.ActionChains Class common methods
click_and_hold(on_element=None) : Left click , Do not release
context_click(on_element=None): Right click
double_click(on_element=None): Double click the left mouse button
drag_and_drop(source, target): Drag to an element and release
move_by_offset(xoffset, yoffset): Move the mouse from the current position to a certain coordinate
move_to_element(to_element) : Move the mouse to an element
release(on_element=None): Release the pressed mouse button on the element
pause(seconds): Suspend operation ( second )
02.ActionChains Class all methods
perform(self)– Execute mouse operation method
reset_actions()– Clear operating instructions
click(on_element=None)– Left click
click_and_hold(on_element=None): Left click , Do not release
context_click(on_element=None): Right click
double_click(on_element=None): Double click the left mouse button
drag_and_drop(source, target): Drag to an element and release
drag_and_drop_by_offset(source, xoffset, yoffset) : Drag to a coordinate and release
key_down(value, element=None): Press... On a keyboard
key_up(value, element=None) : Loosen a
move_by_offset(xoffset, yoffset): Move the mouse from the current position to a certain coordinate
move_to_element(to_element) : Move the mouse to an element
move_to_element_with_offset(to_element, xoffset, yoffset): Move to
From an element ( Top left coordinates ) How far away is the location
pause(seconds): Suspend operation ( second )
release(on_element=None): Release the pressed mouse button on the element
send_keys(*keys_to_send): Send a key to the current focus element
send_keys_to_element(element, *keys_to_send) : Send a key to the specified element
03.ActionChains Use steps
- Instantiation :actions = ActionChains(driver)
- Call the mouse operation method :actions.move_to_element(menu)
- Execute mouse operation method :actions.perform()
04. actual combat
What the code does :
- Open the test web page :https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover
- Switch iframe
- Navigate to the bound mouse event div
- Move the mouse to div Event triggered on the ( Can be observed div The words on the will change )
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(r"D:\chromeDriver\71\chromedriver71.exe")
driver.implicitly_wait(5)
driver.get(url="https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover")
driver.maximize_window()
# Switch iframe
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="iframeResult"]'))
# Element localization
ele = driver.find_element_by_xpath('//div[@οnmοuseοver="mOver(this)"]')
# Move the mouse over the element to trigger the event
actions = ActionChains(driver)
actions.move_to_element(ele)
actions.perform()
time.sleep(5)
driver.quit()
05. summary
ActionChains Other methods in the class are used in the same way , If you have time, you can try
ActionChains There are many methods in the class , But not many are commonly used , Listed above , Just master the common methods
Particular attention :ActionChains Implementation principle of , When you call ActionChains Method time , Not immediately , Instead, all operations are put in a queue in order , When you call perform() When the method is used , The time in the queue will execute in turn .
ActionChains Methods in a class can be called using a chain , Let's think about our own expansion ( It's very simple ).
Finally, thank everyone who reads my article carefully , The following online link is also a very comprehensive one that I spent a few days sorting out , I hope it can also help you in need !
These materials , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….
边栏推荐
- [200 opencv routines] 216 Draw polylines and polygons
- ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
- 2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
- 2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
- Embedded development: five revision control best practices
- For the sustainable development of software testing, we must learn to knock code?
- 开机时小键盘灯不亮的解决方案
- How does win11 set user permissions? Win11 method of setting user permissions
- Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
- What time do you get off work?!!!
猜你喜欢
![[每日一氵]Latex 的通讯作者怎么搞](/img/0f/d19b27dc42124c89993dee1bada838.png)
[每日一氵]Latex 的通讯作者怎么搞

2023 spring recruitment Internship - personal interview process and face-to-face experience sharing

idea启动Command line is too long问题处理

电脑照片尺寸如何调整成自己想要的

【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)

Where should older test / development programmers go? Will it be abandoned by the times?

Tensorflow team: we haven't been abandoned

DO280管理应用部署--pod调度控制

【Hot100】20. 有效的括号

process. env. NODE_ ENV
随机推荐
Detailed explanation of stm32adc analog / digital conversion
ABAP call restful API
Win11如何設置用戶權限?Win11設置用戶權限的方法
How to adjust the size of computer photos to what you want
开机时小键盘灯不亮的解决方案
Pico, do you want to save or bring consumer VR?
Overview | slam of laser and vision fusion
Day 3 of rhcsa study
Pocket network supports moonbeam and Moonriver RPC layers
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Smart Party Building: faith through time and space | 7.1 dedication
工厂高精度定位管理系统,数字化安全生产管理
How to adjust the color of the computer screen and how to change the color of the computer screen
表格存储中tablestore 目前支持mysql哪些函数呢?
ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods
新出生的机器狗,打滚1小时后自己掌握走路,吴恩达开山大弟子最新成果
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
【OpenCV 例程200篇】216. 绘制多段线和多边形