当前位置:网站首页>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 …….
边栏推荐
- The picgo shortcut is amazing. This person thinks exactly the same as me
- Pico,能否拯救消费级VR?
- Pocket network supports moonbeam and Moonriver RPC layers
- vscode 查找 替换 一个文件夹下所有文件的数据
- Preorder, inorder, follow-up of binary tree (non recursive version)
- 你TM到底几点下班?!!!
- STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律
- Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
- Deep operator overloading (2)
- Samsung took the lead in putting 3nm chips into production, and Shanghai's fresh master students can settle directly. Nankai has established a chip science center. Today, more big news is here
猜你喜欢
Pnas: brain and behavior changes of social anxiety patients with empathic embarrassment
Pico,是要拯救还是带偏消费级VR?
马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
Tanabata confession introduction: teach you to use your own profession to say love words, the success rate is 100%, I can only help you here ~ (programmer Series)
并发编程系列之什么是ForkJoin框架?
【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统
Pico, do you want to save or bring consumer VR?
Use Tencent cloud to build a map bed service
Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
超视频时代,什么样的技术会成为底座?
随机推荐
How does win11 set user permissions? Win11 method of setting user permissions
从大湾区“1小时生活圈”看我国智慧交通建设
One revolution, two forces, three links: the "carbon reduction" roadmap behind the industrial energy efficiency improvement action plan
2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
There will be a gap bug when the search box and button are zoomed
最新NLP赛事实践总结!
周少剑,很少见
【LeetCode】43. 字符串相乘
【OpenCV 例程200篇】216. 绘制多段线和多边形
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system
Rhcsa fourth day operation
HR面试:最常见的面试问题和技巧性答复
马来西亚《星报》:在WTO MC12 孙宇晨仍在坚持数字经济梦想
RT-Thread Env 工具介绍(学习笔记)
Smart Party Building: faith through time and space | 7.1 dedication
[daily news]what happened to the corresponding author of latex
Task.Run(), Task.Factory.StartNew() 和 New Task() 的行为不一致分析
Embedded development: five revision control best practices
There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode