selenium-webdriver(python) (十五) -- 鼠标事件

简介:

本节重点:

ActionChains 类

  •   context_click()  右击
  •   double_click()   双击
  •   drag_and_drop()  拖动

 

测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单,可以方便的选择快捷菜单中的选择对文件进行操作(删除、移动、重命名),之前学习元素的点击非常简单:

driver.find_element_by_id(“xxx”).click()

那么鼠标的双击、右击、拖动等是否也是这样的写法呢?例如右击:

driver.find_element_by_id(“xxx”).context_click()

经过运行脚本得到了下面的错误提示:

AttributeError: 'WebElement' object has no attribute 'context_click' 

提示右点方法不属于webelement 对象,通过查找文档,发现属于ActionChains 类,但文档中没有具体写法。这里要感谢 北京-QC-rabbit 的指点,其实整个python+selenium 学习过程都要感谢 北京-QC-rabbit 的指点。

 

 

下面介绍鼠标右键的用法,以快播私有云为例:

#coding=utf-8

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")

#登陆快播私有云
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").send_keys("123456")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)

#定位到要右击的元素
qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(qqq).perform()


'''
#你也可以使用三行的写法,但我觉得上面两行写法更容易理解
chain = ActionChains(driver)
implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
chain.context_click(implement).perform()
'''

time.sleep(3) #休眠3秒
driver.close()

这里需要注意的是,在使用ActionChains 类之前,要先将包引入。

 

 

右击的操作会了,下面的其它方法比葫芦画瓢也能写出来。

鼠标双击的写法:

#定位到要双击的元素
qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()

 

 

鼠标拖放操作的写法:

#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
target =  driver.find_element_by_name("target")

#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()

 

ActionChains 类不仅仅是只包含了上面的三个方法,下面将方法列出:

class ActionChains(driver)

driver:The WebDriver instance which performs user actions.

Generate user actions. All actions are stored in the ActionChains object. Call perform() to fire stored actions.

 

  – perform()

Performs all stored actions.

 

  – click(on_element=None)

Clicks an element.

on_element:The element to click. If None, clicks on current mouse position.

 

  – click_and_hold(on_element)

Holds down the left mouse button on an element.

on_element:The element to mouse down. If None, clicks on current mouse position.

 

  – context_click(on_element)

Performs a context-click (right click) on an element.

on_element:The element to context-click. If None, clicks on current mouse position.

 

  – double_click(on_element)

Double-clicks an element.

on_element:The element to double-click. If None, clicks on current mouse position.

  

  – drag_and_drop(source, target)

Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.

source:The element to mouse down.

target: The element to mouse up.

 

  – key_down(key, element=None)

Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt andShift).

key:The modifier key to send. Values are defined in Keys class.

element:The element to send keys. If None, sends a key to current focused element.

  – key_up(key, element=None)

Releases a modifier key.

key:The modifier key to send. Values are defined in Keys class.

element:The element to send keys. If None, sends a key to current focused element.

 

  – move_by_offset(xoffset, yoffset)

Moving the mouse to an offset from current mouse position.

xoffset:X offset to move to.yoffset:Y offset to move to.

 

  – move_to_element(to_element)

Moving the mouse to the middle of an element.

to_element: The element to move to.

 

  – move_to_element_with_offset(to_element, xoffset, yoffset)

Move the mouse by an offset of the specificed element. Offsets are relative to the top-left corner of the

element.

to_element: The element to move to.xoffset:X offset to move to.yoffset:Y offset to move to.

 

  – release(on_element)

Releasing a held mouse button.

on_element:The element to mouse up.

 

  – send_keys(*keys_to_send)

Sends keys to current focused element.

keys_to_send:The keys to send.

 

  – send_keys_to_element(self, element,*keys_to_send):

Sends keys to an element.

element:The element to send keys.keys_to_send:The keys to send.

 

目录
相关文章
|
11天前
|
Web App开发 前端开发 JavaScript
探索Python科学计算的边界:利用Selenium进行Web应用性能测试与优化
【10月更文挑战第6天】随着互联网技术的发展,Web应用程序已经成为人们日常生活和工作中不可或缺的一部分。这些应用不仅需要提供丰富的功能,还必须具备良好的性能表现以保证用户体验。性能测试是确保Web应用能够快速响应用户请求并处理大量并发访问的关键步骤之一。本文将探讨如何使用Python结合Selenium来进行Web应用的性能测试,并通过实际代码示例展示如何识别瓶颈及优化应用。
42 5
|
2月前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
48 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
|
10天前
|
数据采集 Web App开发 数据可视化
Python爬虫教程:Selenium可视化爬虫的快速入门
Python爬虫教程:Selenium可视化爬虫的快速入门
|
21天前
|
测试技术 数据安全/隐私保护 开发者
自动化测试的奥秘:如何用Selenium和Python提升软件质量
【9月更文挑战第35天】在软件开发的海洋中,自动化测试是那艘能引领我们穿越波涛的帆船。本文将揭开自动化测试的神秘面纱,以Selenium和Python为工具,展示如何构建一个简单而强大的自动化测试框架。我们将从基础出发,逐步深入到高级应用,让读者能够理解并实现自动化测试脚本,从而提升软件的质量与可靠性。
|
1月前
|
Web App开发 测试技术 持续交付
自动化测试的利器:Selenium与Python的完美结合
【9月更文挑战第21天】在软件开发的世界里,测试是确保产品质量的关键步骤。随着敏捷开发和持续集成的流行,自动化测试工具变得尤为重要。本文将介绍如何使用Selenium和Python进行高效的自动化测试,不仅提供代码示例,还深入探讨如何设计测试用例、选择正确的测试框架、以及如何整合到CI/CD流程中。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用的技巧。
41 3
|
2月前
|
数据采集 人工智能 数据可视化
Python selenium爬虫被检测到,该怎么破?
Python selenium爬虫被检测到,该怎么破?
|
2月前
|
Web App开发 测试技术 API
自动化测试之美:使用Selenium和Python进行Web应用测试
【8月更文挑战第31天】在软件开发的快节奏世界中,自动化测试如同一束明灯,照亮了质量保证之路。本文将引导你通过Selenium和Python的强大组合,探索如何构建高效的Web应用测试框架。我们不仅会讨论理论,还会深入代码,从一个简单的示例开始,逐步扩展至更复杂的场景。无论你是初学者还是有经验的开发者,这篇文章都将为你提供宝贵的见解和实用的技巧。让我们一同揭开自动化测试的神秘面纱,体验它的魅力所在。
|
2月前
|
Web App开发 XML 测试技术
自动化测试框架设计:以Python和Selenium为例
【8月更文挑战第31天】在软件开发的快节奏中,自动化测试成为确保产品质量的关键步骤。本文将引导读者了解如何结合Python语言和Selenium工具来设计一个高效的自动化测试框架。通过浅显易懂的语言和实际代码示例,我们将探索自动化测试框架的核心组件,并学习如何实现它们。无论你是测试新手还是希望提升自动化技能的开发者,这篇文章都将为你打开一扇通向高效软件测试的大门。
|
2月前
|
敏捷开发 测试技术 数据安全/隐私保护
自动化测试的高效之路:如何利用Python和Selenium提升测试效率
【8月更文挑战第28天】本文旨在探讨通过Python语言结合Selenium框架来提高软件测试的效率。文章不仅介绍了自动化测试的基本概念,还提供了具体的代码示例,帮助读者理解如何实现自动化测试脚本,并指出了在实施过程中可能遇到的问题及其解决方案。通过本文,读者将学会如何有效地使用Python和Selenium工具,以减少重复性工作,提升测试流程的效率与准确性。
|
1月前
|
敏捷开发 测试技术 持续交付
自动化测试之美:如何用Selenium和Python打造高效测试脚本
【9月更文挑战第13天】在软件开发的海洋中,自动化测试是那抹不可或缺的亮色。它不仅提升了测试效率,还保障了产品质量。本文将带你领略使用Selenium和Python构建自动化测试脚本的魅力所在,从环境的搭建到脚本的编写,再到问题的排查,每一步都是对软件质量把控的深刻理解和实践。让我们开始这段探索之旅,解锁自动化测试的秘密吧!
44 0

热门文章

最新文章