当前位置:网站首页>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 …….
边栏推荐
- 实现数字永生还有多久?元宇宙全息真人分身#8i
- ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
- [PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
- Pico,能否拯救消费级VR?
- 【Pygame实战】你说神奇不神奇?吃豆人+切水果结合出一款你没玩过的新游戏!(附源码)
- 她就是那个「别人家的HR」|ONES 人物
- 从大湾区“1小时生活圈”看我国智慧交通建设
- 高端程序员上班摸鱼指南
- 大龄测试/开发程序员该何去何从?是否会被时代抛弃?
- Detailed explanation of stm32adc analog / digital conversion
猜你喜欢

七夕表白攻略:教你用自己的专业说情话,成功率100%,我只能帮你们到这里了啊~(程序员系列)

picgo快捷键 绝了这人和我的想法 一模一样

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

【开源数据】基于虚拟现实场景的跨模态(磁共振、脑磁图、眼动)人类空间记忆研究开源数据集

6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary

有些能力,是工作中学不来的,看看这篇超过90%同行

【Hot100】20. 有效的括号

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices

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

Summer Challenge harmonyos canvas realize clock
随机推荐
药品溯源夯实安全大堤
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
【Hot100】20. 有效的括号
高端程序员上班摸鱼指南
【每日一题】1175. 质数排列
process.env.NODE_ENV
STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
Vscode find and replace the data of all files in a folder
MySQL advanced 4
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
Solution to the problem that the keypad light does not light up when starting up
Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
Some abilities can't be learned from work. Look at this article, more than 90% of peers
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
【IDM】IDM下载器安装
[video memory optimization] deep learning video memory optimization method
DO280管理应用部署--pod调度控制
【Hot100】17. 电话号码的字母组合
C#/VB.NET 合并PDF文档
Nuxt. JS data prefetching