当前位置:网站首页>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 …….
边栏推荐
- 从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
- Win11如何設置用戶權限?Win11設置用戶權限的方法
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- 一次革命、两股力量、三大环节:《工业能效提升行动计划》背后的“减碳”路线图...
- How does win11 set user permissions? Win11 method of setting user permissions
- 运动捕捉系统原理
- 6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
- 【每日一题】1175. 质数排列
- 动作捕捉系统用于苹果采摘机器人
- Which MySQL functions are currently supported by tablestore in table storage?
猜你喜欢
What is the forkjoin framework in the concurrent programming series?
The newly born robot dog can walk by himself after rolling for an hour. The latest achievement of Wu Enda's eldest disciple
TensorFlow團隊:我們沒被拋弃
Win11如何设置用户权限?Win11设置用户权限的方法
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
Summer Challenge harmonyos canvas realize clock
你TM到底几点下班?!!!
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
随机推荐
How to write good code - Defensive Programming Guide
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
How to adjust the size of computer photos to what you want
Seate中用了shardingjdbc 就不能用全局事务了吗?
process.env.NODE_ENV
分享在大疆DJI(深圳总部)工作的日常和福利
你TM到底几点下班?!!!
Sales management system of lightweight enterprises based on PHP
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
Summer Challenge harmonyos canvas realize clock
ABAP-调用Restful API
【Hot100】20. 有效的括号
Idea start command line is too long problem handling
Rhcsa fourth day operation
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
开机时小键盘灯不亮的解决方案
Nuxt.js数据预取
For the sustainable development of software testing, we must learn to knock code?
Please, stop painting star! This has nothing to do with patriotism!
HR interview: the most common interview questions and technical answers