python+selenium 鼠标事件操作方法


Posted in Python onAugust 24, 2019

一、前言

除了可以使用 click( ) 来模拟鼠标的单击操作,现在Web产品中还提供了更丰富的鼠标交互方式,例如鼠标右键、双击、悬停、拖动等功能,在WebDriver中,将这些关于鼠标操作的方法都封装在 ActionChains 类中。

ActionChains 类提供了鼠标操作的常用方法:

perform() 执行所有ActionChains中存储的行为
context_click() 右击
double_click() 双击
drag_and_drop() 拖动
move_to_element() 鼠标悬停

二、详细使用

1.鼠标右击操作

from selenium import webdriver
#引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
 
driver = webdriver.Chrome()
driver.get("url")
#......
#定位到需要右击的元素
right_click = driver.find_element_by_id("id")
#对元素进行右击操作
ActionChains(driver).context_click(right_click).perform()
#......

ActionChains(driver):调用ActionChains类,将浏览器驱动driver作为参数传入;

perform():执行所有ActionChains中存储的行为,可以理解成是对整个操作的提交动作;

2.鼠标悬停

move_to_element()方法可以模拟鼠标悬停的动作,其用法与context_click()相同;

from selenium import webdriver
#引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
 
driver = webdriver.Chrome()
driver.get("url")
#......
#定位到需要悬停的元素
above = driver.find_element_by_id("id")
#对元素进行右击操作
ActionChains(driver).move_to_element(above).perform()
#......

3.鼠标双击

double_click() 方法用于模拟鼠标双击操作;

from selenium import webdriver
#引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
 
driver = webdriver.Chrome()
driver.get("url")
#......
#定位到需要双击的元素
double_click = driver.find_element_by_id("id")
#对元素进行右击操作
ActionChains(driver).double_click(double_click).perform()
#......

4.鼠标拖动操作

drag_and_drop(source,target) 在源位置元素上按住鼠标左键,然后移动到目标元素上释放。

source:鼠标拖动的源元素

target:鼠标释放的目标元素

from selenium import webdriver
#引入ActionChains类
from selenium.webdriver.common.action_chains import ActionChains
 
driver = webdriver.Chrome()
driver.get("url")
#......
#定位元素的源位置
source = driver.find_element_by_id("id")
#定位元素要移到到的目标位置
target = driver.find_element_by_id("id")
#对元素进行拖动操作
ActionChains(driver).drag_and_drop(source,target).perform()
#......

以上这篇python+selenium 鼠标事件操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python设置检查点简单实现代码
Jul 01 Python
python将文本转换成图片输出的方法
Apr 28 Python
在Python的web框架中编写创建日志的程序的教程
Apr 30 Python
Python计算已经过去多少个周末的方法
Jul 25 Python
Python正则表达式经典入门教程
May 22 Python
Python标准库sched模块使用指南
Jul 06 Python
python调用百度地图WEB服务API获取地点对应坐标值
Jan 16 Python
浅谈pytorch grad_fn以及权重梯度不更新的问题
Aug 20 Python
Python 单例设计模式用法实例分析
Sep 23 Python
python爬虫多次请求超时的几种重试方法(6种)
Dec 01 Python
python matplotlib工具栏源码探析二之添加、删除内置工具项的案例
Feb 25 Python
python在package下继续嵌套一个package
Apr 14 Python
python+selenium select下拉选择框定位处理方法
Aug 24 #Python
Python封装成可带参数的EXE安装包实例
Aug 24 #Python
python识别文字(基于tesseract)代码实例
Aug 24 #Python
python图片二值化提高识别率代码实例
Aug 24 #Python
关于Python形参打包与解包小技巧分享
Aug 24 #Python
python-序列解包(对可迭代元素的快速取值方法)
Aug 24 #Python
对python中的装包与解包实例详解
Aug 24 #Python
You might like
从一个不错的留言本弄的mysql数据库操作类
2007/09/02 PHP
解析php获取字符串的编码格式的方法(函数)
2013/06/21 PHP
PHP实现将浏览历史页面网址保存到cookie的方法
2015/01/26 PHP
php通过递归方式复制目录和子目录的方法
2015/03/13 PHP
jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
2010/06/28 Javascript
Javascript类库的顶层对象名用户体验分析
2010/10/24 Javascript
javascript 三种方法实现获得和设置以及移除元素属性
2013/03/20 Javascript
jQuery固定浮动侧边栏实现思路及代码
2014/09/28 Javascript
JavaScript中操作字符串小结
2015/05/04 Javascript
JQuery实现图片轮播效果
2015/09/15 Javascript
javascript创建对象、对象继承的实用方式详解
2016/03/08 Javascript
BootstrapValidator不触发校验的实现代码
2016/09/28 Javascript
解决Vue中mounted钩子函数获取节点高度出错问题
2018/05/18 Javascript
详解如何在微信小程序中愉快地使用sass
2018/07/30 Javascript
微信小程序使用map组件实现路线规划功能示例
2019/01/22 Javascript
微信小程序自定义toast组件的方法详解【含动画】
2019/05/11 Javascript
微信小程序自定义头部导航栏(组件化)
2019/11/15 Javascript
vue使用openlayers实现移动点动画
2020/09/24 Javascript
从零学python系列之数据处理编程实例(二)
2014/05/22 Python
Python函数中的函数(闭包)用法实例
2016/03/15 Python
PyQt实现界面翻转切换效果
2018/04/20 Python
python pyinstaller 加载ui路径方法
2019/06/10 Python
Django中Middleware中的函数详解
2019/07/18 Python
django列表筛选功能的实现代码
2020/03/27 Python
Python如何爬取b站热门视频并导入Excel
2020/08/10 Python
python之语音识别speech模块
2020/09/09 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
2021/01/12 Python
需要知道的CSS3动画技术
2010/01/01 HTML / CSS
英国的知名精品百货公司:House of Fraser(福来德)
2016/08/14 全球购物
Move Free官方海外旗舰店:美国骨关节健康专业品牌
2017/12/06 全球购物
成人大专生实习期的自我评价
2013/10/02 职场文书
自荐书范文
2013/12/08 职场文书
先进集体事迹材料
2014/02/17 职场文书
公司2015年终工作总结
2015/05/26 职场文书
导游词之太行山青龙峡
2020/01/14 职场文书
python基于tkinter制作下班倒计时工具
2021/04/28 Python