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 相关文章推荐
pyqt4教程之实现windows窗口小示例分享
Mar 07 Python
Python的Flask框架中Flask-Admin库的简单入门指引
Apr 07 Python
Python while 循环使用的简单实例
Jun 08 Python
Python read函数按字节(字符)读取文件的实现
Jul 03 Python
如何使用python实现模拟鼠标点击
Jan 06 Python
tensorflow查看ckpt各节点名称实例
Jan 21 Python
Flask和pyecharts实现动态数据可视化
Feb 26 Python
Python range与enumerate函数区别解析
Feb 28 Python
使用python实现微信小程序自动签到功能
Apr 27 Python
Python变量格式化输出实现原理解析
Aug 06 Python
基于python实现简单C/S模式代码实例
Sep 14 Python
python中的列表和元组区别分析
Dec 30 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
玩家交还《星际争霸》原始码光盘 暴雪报以厚礼
2017/05/05 星际争霸
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
2008/12/06 PHP
PHP 7.0.2 正式版发布
2016/01/08 PHP
PHP simplexml_load_string()函数实例讲解
2019/02/03 PHP
通过jquery实现tab标签浏览效果
2007/02/20 Javascript
JavaScript对象的property属性详解
2014/04/01 Javascript
jQuery实现的五子棋游戏实例
2015/06/13 Javascript
jQuery回到顶部的代码
2016/07/09 Javascript
AngularJS 自定义指令详解及示例代码
2016/08/17 Javascript
如何在Angular.JS中接收并下载PDF
2016/11/26 Javascript
JS多文件上传的实例代码
2017/01/11 Javascript
Vuepress 搭建带评论功能的静态博客的实现
2019/02/17 Javascript
python计数排序和基数排序算法实例
2014/04/25 Python
从零学Python之入门(四)运算
2014/05/27 Python
在Python中进行自动化单元测试的教程
2015/04/15 Python
详解Django框架中的视图级缓存
2015/07/23 Python
Python松散正则表达式用法分析
2016/04/29 Python
深入理解Python中装饰器的用法
2016/06/28 Python
Python中断言Assertion的一些改进方案
2016/10/27 Python
简单谈谈Python的pycurl模块
2018/04/07 Python
Python 日期区间处理 (本周本月上周上月...)
2019/08/08 Python
Django 实现外键去除自动添加的后缀‘_id’
2019/11/15 Python
Python文本处理简单易懂方法解析
2019/12/19 Python
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
2020/01/18 Python
pycharm运行程序时看不到任何结果显示的解决
2020/02/21 Python
Python设计密码强度校验程序
2020/07/30 Python
html5通过postMessage进行跨域通信的方法
2017/12/04 HTML / CSS
C#里面如何判断一个Object是否是某种类型(如Boolean)?
2016/02/10 面试题
法学研究生自我鉴定范文
2013/12/04 职场文书
办理信用卡工作证明
2014/01/11 职场文书
党员创先争优承诺书
2014/03/26 职场文书
超市仓管员岗位职责
2014/04/07 职场文书
优秀乡村医生先进事迹材料
2014/08/23 职场文书
民事撤诉申请书范本
2015/05/18 职场文书
申请吧主发表的感言
2015/08/03 职场文书
mysql全面解析json/数组
2022/07/07 MySQL