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类的定义、继承及类对象使用方法简明教程
May 08 Python
python 删除大文件中的某一行(最有效率的方法)
Aug 19 Python
PyQt5 QSerialPort子线程操作的实现
Apr 21 Python
Jupyter notebook远程访问服务器的方法
May 24 Python
Python闭包执行时值的传递方式实例分析
Jun 04 Python
基于Pandas读取csv文件Error的总结
Jun 15 Python
Python split() 函数拆分字符串将字符串转化为列的方法
Jul 16 Python
Python发送邮件的实例代码讲解
Oct 16 Python
解决pycharm下pyuic工具使用的问题
Apr 08 Python
python正则表达式的懒惰匹配和贪婪匹配说明
Jul 13 Python
python绘制箱型图
Apr 27 Python
用Python爬取英雄联盟的皮肤详细示例
Dec 06 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的可变变量名需要的注意的问题
2013/06/20 PHP
PHP加密扩展库Mcrypt安装和实例
2013/11/10 PHP
preg_match_all使用心得分享
2014/01/31 PHP
2个Codeigniter文件批量上传控制器写法例子
2014/07/25 PHP
php 后端实现JWT认证方法示例
2018/09/04 PHP
如何在PHP环境中使用ProtoBuf数据格式
2020/06/19 PHP
理解JavaScript中的对象 推荐
2011/01/09 Javascript
jQuery数组处理代码详解(含实例演示)
2012/02/03 Javascript
jQuery实现随意改变div任意属性的名称和值(部分原生js实现)
2013/05/28 Javascript
jquery基础教程之deferred对象使用方法
2014/01/22 Javascript
fullpage.js全屏滚动插件使用实例
2016/09/06 Javascript
jquery animate动画持续运动的实例
2017/11/29 jQuery
js使用ajax传值给后台,后台返回字符串处理方法
2018/08/08 Javascript
H5+C3+JS实现五子棋游戏(AI篇)
2020/05/28 Javascript
layui实现数据分页功能(ajax异步)
2019/07/27 Javascript
在JavaScript中实现链式调用的实现
2019/12/24 Javascript
Vue 实现分页与输入框关键字筛选功能
2020/01/02 Javascript
Vue ​v-model相关知识总结
2021/01/28 Vue.js
Python中逗号的三种作用实例分析
2015/06/08 Python
python算法演练_One Rule 算法(详解)
2017/05/17 Python
使用python实现个性化词云的方法
2017/06/16 Python
Python使用pickle模块报错EOFError Ran out of input的解决方法
2018/08/16 Python
python爬虫 urllib模块反爬虫机制UA详解
2019/08/20 Python
CSS3下的渐变文字效果实现示例
2018/03/02 HTML / CSS
Html5适配iphoneX刘海屏的简单实现
2019/04/09 HTML / CSS
Fanatics英国官网:美国体育电商
2018/11/06 全球购物
简述DNS进行域名解析的过程
2013/12/02 面试题
什么是GWT的Entry Point
2013/08/16 面试题
区优秀教师事迹材料
2014/02/10 职场文书
致铅球运动员加油稿
2014/02/13 职场文书
中药专业自荐信范文
2014/03/18 职场文书
三年级上册科学教学计划
2015/01/21 职场文书
员工表扬信怎么写
2015/05/05 职场文书
安全学习心得体会范文
2016/01/18 职场文书
使用Python脚本对GiteePages进行一键部署的使用说明
2021/05/27 Python
Python标准库pathlib操作目录和文件
2021/11/20 Python