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 相关文章推荐
django之常用命令详解
Jun 30 Python
python 自动化将markdown文件转成html文件的方法
Sep 23 Python
Python算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
python+numpy+matplotalib实现梯度下降法
Aug 31 Python
PyCharm+PySpark远程调试的环境配置的方法
Nov 29 Python
PyQt编程之如何在屏幕中央显示窗体的实例
Jun 18 Python
python实现布隆过滤器及原理解析
Dec 08 Python
PyCharm永久激活方式(推荐)
Sep 22 Python
在PyTorch中使用标签平滑正则化的问题
Apr 03 Python
Python特殊属性property原理及使用方法解析
Oct 09 Python
selenium3.0+python之环境搭建的方法步骤
Feb 01 Python
Python OpenCV实现图形检测示例详解
Apr 08 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
php中实现精确设置session过期时间的方法
2014/07/17 PHP
PHP中isset与array_key_exists的区别实例分析
2015/06/02 PHP
PHP实现对图片的反色处理功能【测试可用】
2018/02/01 PHP
javascript+css 网页每次加载不同样式的实现方法
2009/12/27 Javascript
jquery UI Datepicker时间控件的使用方法(加强版)
2015/11/07 Javascript
为什么JavaScript没有块级作用域
2016/05/22 Javascript
BootStrap中Datetimepicker和uploadify插件应用实例小结
2016/05/26 Javascript
js 判断一组日期是否是连续的简单实例
2016/07/11 Javascript
jQuery Raty 一款不错的星级评分插件
2016/08/24 Javascript
JavaScript 中 apply 、call 的详解
2017/03/21 Javascript
bootstrap table表格使用方法详解
2017/04/26 Javascript
详解Layer弹出层样式
2017/08/21 Javascript
Angular利用trackBy提升性能的方法
2018/01/26 Javascript
ES6的解构赋值实例详解
2019/05/06 Javascript
javascript sort()对数组中的元素进行排序详解
2019/10/13 Javascript
Element el-button 按钮组件的使用详解
2021/02/01 Javascript
js闭包和垃圾回收机制示例详解
2021/03/01 Javascript
Python实现计算最小编辑距离
2016/03/17 Python
python实现学生管理系统
2018/01/11 Python
Ubuntu下Anaconda和Pycharm配置方法详解
2019/06/14 Python
Python 实现遥感影像波段组合的示例代码
2019/08/04 Python
Matplotlib scatter绘制散点图的方法实现
2020/01/02 Python
解决ROC曲线画出来只有一个点的问题
2020/02/28 Python
css3实现元素环绕中心点布局的方法示例
2019/01/15 HTML / CSS
美国美食礼品篮网站:Gourmet Gift Baskets
2019/12/15 全球购物
数控技术专科生自我评价
2014/01/08 职场文书
机械专业应届毕业生自荐书
2014/06/12 职场文书
校长师德师风自我剖析材料
2014/09/29 职场文书
2014年安全工作总结范文
2014/11/13 职场文书
2016春节家属慰问信
2015/03/25 职场文书
二十年同学聚会致辞
2015/07/28 职场文书
小学英语教师研修感悟
2015/11/18 职场文书
Jedis操作Redis实现模拟验证码发送功能
2021/09/25 Redis
python 管理系统实现mysql交互的示例代码
2021/12/06 Python
利用Python实现翻译HTML中的文本字符串
2022/06/21 Python
Android学习之BottomSheetDialog组件的使用
2022/06/21 Java/Android