当前位置:网站首页>学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
2022-07-01 15:50:00 【TEST_二 黑】
前言
我们在做 Web 自动化的时候,有时候页面的元素不需要我们点击,值需要把鼠标移动上去就能展示各种信息。
这个时候我们可以通过操作鼠标来实现,接下来我们来讲一下使用 selenium 做 Web 自动化的时候如何来操作鼠标。
鼠标操作,我们可以使用 selenium 的 ActionChains 类来实现,我们先来认识一下这个类。
01.ActionChains 类常用方法
click_and_hold(on_element=None) :点击鼠标左键,不松开
context_click(on_element=None):点击鼠标右键
double_click(on_element=None):双击鼠标左键
drag_and_drop(source, target):拖拽到某个元素然后松开
move_by_offset(xoffset, yoffset):鼠标从当前位置移动到某个坐标
move_to_element(to_element) :鼠标移动到某个元素
release(on_element=None):在元素上释放按住的鼠标按钮
pause(seconds):暂停操作(秒)
02.ActionChains 类所有方法
perform(self)–执行鼠标操作方法
reset_actions()–清楚操作子令
click(on_element=None)–点击鼠标左键
click_and_hold(on_element=None):点击鼠标左键,不松开
context_click(on_element=None):点击鼠标右键
double_click(on_element=None):双击鼠标左键
drag_and_drop(source, target):拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) :拖拽到某个坐标然后松开
key_down(value, element=None):按下某个键盘上的
key_up(value, element=None) :松开某个
move_by_offset(xoffset, yoffset):鼠标从当前位置移动到某个坐标
move_to_element(to_element) :鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset):移动到
距某个元素(左上角坐标)多少距离的位置
pause(seconds):暂停操作(秒)
release(on_element=None):在元素上释放按住的鼠标按钮
send_keys(*keys_to_send):发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) :发送某个键到指定元素
03.ActionChains 使用步骤
- 实例化:actions = ActionChains(driver)
- 调用鼠标操作方法:actions.move_to_element(menu)
- 执行鼠标操作方法:actions.perform()
04.实战
代码做的事情:
- 打开测试网页:https://www.runoob.com/try/try.php?filename=tryjs_events_mouseover
- 切换 iframe
- 定位到绑定鼠标事件的 div
- 将鼠标移动到 div 上触发事件(可以观察到 div 上字会发生变化)
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()
#切换iframe
driver.switch_to.frame(driver.find_element_by_xpath('//iframe[@id="iframeResult"]'))
#元素定位
ele = driver.find_element_by_xpath('//div[@οnmοuseοver="mOver(this)"]')
#移动鼠标到元素上触发事件
actions = ActionChains(driver)
actions.move_to_element(ele)
actions.perform()
time.sleep(5)
driver.quit()
05.总结
ActionChains 类中其他方法使用方式都一样,大家有空可以试一下
ActionChains 类中有很多方法,但是常用的并不多,上面已经列出,掌握常用方法即可
特别注意:ActionChains 的执行原理,当你调用 ActionChains 的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个队列里,当你调用 perform()方法时,队列中的时间会依次执行。
ActionChains 类中的方法可以使用链式调用,大家思考思考自己拓展吧(其实很简单)。
最后感谢每一个认真阅读我文章的人,下面这个网盘链接也是我费了几天时间整理的非常全面的,希望也能帮助到有需要的你!
这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
边栏推荐
- process.env.NODE_ENV
- 三星率先投产3nm芯片,上海应届硕士生可直接落户,南开成立芯片科学中心,今日更多大新闻在此...
- 6.2 normalization 6.2.6 BC normal form (BCNF) 6.2.9 normalization summary
- 电脑照片尺寸如何调整成自己想要的
- 工厂高精度定位管理系统,数字化安全生产管理
- Day 3 of rhcsa study
- There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- AVL balanced binary search tree
- [target tracking] |stark
猜你喜欢

ATSs: automatically select samples to eliminate the difference between anchor based and anchor free object detection methods

HR面试:最常见的面试问题和技巧性答复
![[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)](/img/0a/c1a4b57b9729e0cf9de1feae9f8c19.png)
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)

【LeetCode】43. String multiplication

智慧党建: 穿越时空的信仰 | 7·1 献礼

综述 | 激光与视觉融合SLAM

2023届春招实习-个人面试过程和面经分享

近半年内连获5家“巨头”投资,这家智能驾驶“黑马”受资本追捧

AVL 平衡二叉搜索树

DO280管理应用部署--pod调度控制
随机推荐
揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
Équipe tensflow: Nous ne sommes pas abandonnés
[pyGame practice] do you think it's magical? Pac Man + cutting fruit combine to create a new game you haven't played! (source code attached)
开机时小键盘灯不亮的解决方案
MySQL高级篇4
Go语学习笔记 - gorm使用 - 表增删改查 | Web框架Gin(八)
Redis seckill demo
运动捕捉系统原理
[每日一氵]Latex 的通讯作者怎么搞
有些能力,是工作中学不来的,看看这篇超过90%同行
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件
Microservice tracking SQL (support Gorm query tracking under isto control)
自動、智能、可視!深信服SSLO方案背後的八大設計
How to adjust the size of computer photos to what you want
Some abilities can't be learned from work. Look at this article, more than 90% of peers
ABAP-屏幕切换时,刷新上一个屏幕
Seate中用了shardingjdbc 就不能用全局事务了吗?
求求你们,别再刷 Star 了!这跟“爱国”没关系!
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
[PHP graduation design] design and implementation of textbook management system based on php+mysql+apache (graduation thesis + program source code) -- textbook management system