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 相关文章推荐
Python3.0与2.X版本的区别实例分析
Aug 25 Python
python PIL模块与随机生成中文验证码
Feb 27 Python
Python简单实现控制电脑的方法
Jan 22 Python
详谈套接字中SO_REUSEPORT和SO_REUSEADDR的区别
Apr 28 Python
Python多进程原理与用法分析
Aug 21 Python
Python数据可视化 pyecharts实现各种统计图表过程详解
Aug 15 Python
python实现全排列代码(回溯、深度优先搜索)
Feb 26 Python
浅谈python3 构造函数和析构函数
Mar 12 Python
解决Keras TensorFlow 混编中 trainable=False设置无效问题
Jun 28 Python
利用Python实现朋友圈中的九宫格图片效果
Sep 03 Python
Scrapy项目实战之爬取某社区用户详情
Sep 17 Python
Python实现微信表情包炸群功能
Jan 28 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
世界咖啡生产者论坛呼吁:需要立即就咖啡价格采取认真行动
2021/03/06 咖啡文化
PHP与已存在的Java应用程序集成
2006/10/09 PHP
DedeCms模板安装/制作概述
2007/03/11 PHP
PHP时间戳与日期之间转换的实例介绍
2013/04/19 PHP
使用php实现截取指定长度
2013/08/06 PHP
PHP中require和include路径问题详解
2014/12/25 PHP
Laravel中七个非常有用但很少人知道的Carbon方法
2017/09/21 PHP
PHP编程实现的TCP服务端和客户端功能示例
2018/04/13 PHP
IE8 浏览器Cookie的处理
2009/01/31 Javascript
ExtJs 3.1 XmlTreeLoader Example Error
2010/02/09 Javascript
基于Jquery的实现回车键Enter切换焦点
2010/09/14 Javascript
IE6下拉框图层问题探讨及解决
2014/01/03 Javascript
javascript实现动态加载CSS
2015/01/26 Javascript
Nodejs初级阶段之express
2015/11/23 NodeJs
有关suggest快速删除后仍然出现下拉列表的bug问题
2016/12/02 Javascript
jQuery实现CheckBox全选、全不选功能
2017/01/11 Javascript
微信小程序 scroll-view隐藏滚动条详解
2017/01/16 Javascript
原生js实现随机点名功能
2019/11/05 Javascript
vue实现图片按比例缩放问题操作
2020/08/11 Javascript
详解JavaScript中的数据类型,以及检测数据类型的方法
2020/09/17 Javascript
构建一个JavaScript插件系统
2020/10/20 Javascript
[04:13]2018国际邀请赛典藏宝瓶Ⅱ饰品一览
2018/07/21 DOTA
[01:21:07]EG vs Liquid 2018国际邀请赛淘汰赛BO3 第一场 8.25
2018/08/29 DOTA
python批量制作雷达图的实现方法
2016/07/26 Python
Python sqlite3事务处理方法实例分析
2017/06/19 Python
python使用KNN算法手写体识别
2018/02/01 Python
Python爬取个人微信朋友信息操作示例
2018/08/03 Python
详解Python Matplot中文显示完美解决方案
2019/03/07 Python
Python 中Django安装和使用教程详解
2019/07/03 Python
Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现
2019/08/13 Python
Python Web静态服务器非堵塞模式实现方法示例
2019/11/21 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
2020/04/15 Python
用python实现学生管理系统
2020/07/24 Python
外贸业务员的岗位职责
2013/11/23 职场文书
2014年银行工作总结范文
2014/11/12 职场文书
党员带头倡议书
2015/04/29 职场文书