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 fabric实现远程操作和部署示例
Mar 25 Python
在Python的Django框架中编写错误提示页面
Jul 22 Python
python自动翻译实现方法
May 28 Python
Python线程指南详细介绍
Jan 05 Python
python3+PyQt5实现文档打印功能
Apr 24 Python
Python使用__new__()方法为对象分配内存及返回对象的引用示例
Sep 20 Python
wxPython实现整点报时
Nov 18 Python
使用Pytorch来拟合函数方式
Jan 14 Python
Python自动重新加载模块详解(autoreload module)
Apr 01 Python
解决pycharm导入本地py文件时,模块下方出现红色波浪线的问题
Jun 01 Python
解决python3中os.popen()出错的问题
Nov 19 Python
python实现银行账户系统
Feb 22 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创建动态图像
2006/10/09 PHP
php批量删除数据
2007/01/18 PHP
解析PHP中常见的mongodb查询操作
2013/06/20 PHP
php把字符串指定字符分割成数组的方法
2018/03/12 PHP
Laravel5.7框架安装与使用学习笔记图文详解
2019/04/02 PHP
javascript 面向对象,实现namespace,class,继承,重载
2009/10/29 Javascript
Jquery解析json数据详解
2013/12/26 Javascript
讲解JavaScript的Backbone.js框架的MVC结构设计理念
2016/02/14 Javascript
Bootstrap项目实战之子栏目资讯内容
2016/04/25 Javascript
Javascript 链式作用域详细介绍
2017/02/23 Javascript
React学习笔记之条件渲染(一)
2017/07/02 Javascript
vue-cli的eslint相关用法
2017/09/29 Javascript
JS生成随机打乱数组的方法示例
2017/12/23 Javascript
vue自定义正在加载动画的例子
2019/11/14 Javascript
使用vue打包进行云服务器上传的问题
2020/03/02 Javascript
Vant picker 多级联动操作
2020/11/02 Javascript
[54:53]完美世界DOTA2联赛PWL S2 GXR vs PXG 第二场 11.18
2020/11/18 DOTA
关于你不想知道的所有Python3 unicode特性
2014/11/28 Python
详解Python之unittest单元测试代码
2018/01/24 Python
tensorflow入门之训练简单的神经网络方法
2018/02/26 Python
对python requests的content和text方法的区别详解
2018/10/11 Python
python实时获取外部程序输出结果的方法
2019/01/12 Python
python+pyqt5实现KFC点餐收银系统
2019/01/24 Python
python+selenium 定位到元素,无法点击的解决方法
2019/01/30 Python
Python 实现域名解析为ip的方法
2019/02/14 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
Python之虚拟环境virtualenv,pipreqs生成项目依赖第三方包的方法
2019/07/23 Python
Python实现TCP通信的示例代码
2019/09/09 Python
解决TensorFlow调用Keras库函数存在的问题
2020/07/06 Python
浅谈CSS3 动画卡顿解决方案
2019/01/02 HTML / CSS
简历中个人求职的自我评价模板
2013/11/29 职场文书
创业计划书之个人工作室
2019/08/22 职场文书
创业开店,这样方式更合理
2019/08/26 职场文书
Golang 切片(Slice)实现增删改查
2022/04/22 Golang
Python 匹配文本并在其上一行追加文本
2022/05/11 Python
python playwrigh框架入门安装使用
2022/07/23 Python