python右键点击_selenium-webdriver(python) -- 鼠标事件(双击,右键)

本文介绍了如何使用selenium的ActionChains类来实现鼠标右键点击和双击操作。通过示例代码展示了在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:

以下是Python使用seleniumwebdriver进行鼠标和键盘操作的详细列举: ### 鼠标操作 - **单击操作**:通过定位元素,使用`ActionChains`类的`click`方法模拟鼠标单击。示例代码如下: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() driver.get('http://cn.bing.com/') searchButtonElement = driver.find_element_by_id('sb_form_go') ActionChains(driver).click(searchButtonElement).perform() ``` - **双击操作**:使用`ActionChains`类的`double_click`方法模拟鼠标双击。示例如下: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() element = driver.find_element_by_id("123") ActionChains(driver).double_click(element).perform() ``` - **右击操作**:使用`ActionChains`类的`context_click`方法模拟鼠标右击。示例如下: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() r_click = driver.find_element_by_xpath('//a[@class="sabX3Rn"]') ActionChains(driver).context_click(r_click).perform() ``` - **鼠标悬停**:使用`ActionChains`类的`move_to_element`方法模拟鼠标悬停。示例如下: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() move = driver.find_element_by_xpath('//span[@class="DIcOFyb"]/i') ActionChains(driver).move_to_element(move).perform() ``` - **鼠标拖动**:使用`ActionChains`类的`drag_and_drop`方法模拟鼠标拖动。示例如下: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() source = driver.find_element() target = driver.find_element() ActionChains(driver).drag_and_drop(source, target).perform() ``` ### 键盘操作 在Selenium中,键盘操作主要通过`send_keys`方法实现。例如,在输入框中输入文本: ```python from selenium import webdriver driver = webdriver.Chrome() driver.get('http://cn.bing.com/') searchElement = driver.find_element_by_id('sb_form_q') searchElement.send_keys('selenium') ``` 同时,还可以结合`Keys`类模拟一些特殊的键盘按键,如回车键等: ```python from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get('http://cn.bing.com/') searchElement = driver.find_element_by_id('sb_form_q') searchElement.send_keys('selenium') searchElement.send_keys(Keys.RETURN) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值