一、前言

除了可以使用 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()
---------------------
作者:一条漂浮的枫叶
来源:CSDN
原文:https://blog.csdn.net/zb455405775/article/details/80675718
版权声明:本文为博主原创文章,转载请附上博文链接!

python+selenium 鼠标事件操作的更多相关文章

  1. 9 Python+Selenium鼠标事件

    [环境信息] python3.6+Selenium3.0.2+Firefox50.0+win7 [ActionChains类鼠标事件的常用方法] 1.右击:context_click() 2.双击:d ...

  2. python selenium --鼠标事件

    转自:http://www.cnblogs.com/fnng/p/3288444.html 本节重点: ActionChains 类 context_click()  右击 double_click( ...

  3. selenium 鼠标事件操作

    1.操作鼠标事件的类:ActionChains  perform()  执行所有ActionChains中存储的行为 context_click()  右击 double_click()   双击 d ...

  4. python selenium鼠标键盘操作(ActionChains)

    用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件--ActionChains sele ...

  5. python selenium鼠标滑动操作

    先安装pyautogui: pip install pyautogui #coding=utf-8 import pyautogui from selenium import webdriver fr ...

  6. python+selenium_鼠标事件

    引言--在实际的web产品测试中,对于鼠标的操作,不单单只有click(),有时候还要用到右击.双击.拖动等操作,这些操作包含在ActionChains类中. 一.ActionChains类中鼠标操作 ...

  7. python模拟鼠标拖动操作的方法

    本文实例讲述了python模拟鼠标拖动操作的方法.分享给大家供大家参考.具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签.重复的拖动工作实在无趣,还是让程序帮我实现吧 ...

  8. Python+selenium鼠标、键盘事件

    鼠标操作 现在的Web产品提供了丰富的鼠标交互方式,例如鼠标右击.双击.悬停.甚至是鼠标拖动等功能,在Webdriver中,将这些关于鼠标操作的方法封装在ActionChains类提供. 1.鼠标右击 ...

  9. selenium的鼠标事件操作

    自动化测试过程中,经常会用到鼠标事件,在selenium的action_chains模块的ActionChains定义了鼠标操作的一些事件,要使用ActionChains类中的方法,首先需要对Acti ...

随机推荐

  1. Storm编程入门API系列之Storm的Topology默认Workers、默认executors和默认tasks数目

    关于,storm的启动我这里不多说了. 见博客 storm的3节点集群详细启动步骤(非HA和HA)(图文详解) 建立stormDemo项目 Group Id :  zhouls.bigdata Art ...

  2. Unity EditorWindow知识记录

    1.创建EditorWindow using UnityEditor; using UnityEngine; public class ZZEditorWindow : EditorWindow { ...

  3. 7、斐波那契数列、跳台阶、变态跳台阶、矩形覆盖------------>剑指offer系列

    题目:斐波那契数列 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). f(n) = f(n-1) + f(n-2) 基本思路 这道题在剑指offe ...

  4. PeopleSoft FSCM Production Support 案例分析

    PeopleSoft FSCM Production Support 案例分析 2010年的时候曾建言博客园开辟Oracle ERP模块供大家交流,博客园如约开辟Oracle ERP 模块,而我后来却 ...

  5. (转)VIM 一键自动添加文件头注释

    通过修改VIM的配置文件.vimrc可以让Vim(gvim)支持自动添加作者信息,具体代码如下: "进行版权声明的设置 "添加或更新头 map <F4> :call T ...

  6. IOS弹出视图preferredContentSize

    UIViewController.preferredContentSize代理旧方法 contentSizeForViewInPopover. self.contentSizeForViewInPop ...

  7. MySQL性能优化奇技淫巧

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.   2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使 ...

  8. SQLServer查询死锁

    --查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys ...

  9. 在Windows 10删除百度云盘的盘符

    1点击微软图标不放,然后点击R  打开运行命令 2输入  Regedit  进入注册表 3找到以下路径:HKEY_LOCAL_MACHINE  SOFTWARE  Microsoft  Windows ...

  10. Windows server 2012安装oracle11g(32/64位)步骤

    Oracle官方下地址: http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html以下两网址 ...