Python+Selenium练习篇之25-鼠标右键
来源:互联网 发布:ip网络寻呼话筒 编辑:程序博客网 时间:2024/06/04 20:04
本文介绍如何使用Selenium中右键方法来操作网页元素。网页上有些元素是支持右键来触发新的菜单,这里找一个百度首页,百度logo的右键,查看图片动作来模拟这个场景。
问题拆分:
1. 打开百度首页,找到logo,右键鼠标
2. 移动到菜单,查看图像,然后点击
核心问题,如何右键操作,然后是如何选择查看图像这个菜单。在Selenium中,有一个ActionChains模块支持,右键,鼠标悬停,拖拽,双击等动作。我们可以通过键盘向下箭头来选择查看图像这个菜单,然后点击就可以达到目的。
相关脚本代码如下:
# coding=utf-8import timefrom selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()driver.maximize_window()driver.get('https://www.baidu.com')time.sleep(2)element = driver.find_element_by_xpath("//*[@id='lg']/img")actionChains = ActionChains(driver)actionChains.context_click(element).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()#actionChains.context_click(element).send_keys('i').perform()
注意:
ActionChains下相关方法在当前的firefox不工作,这个是一个已知的bug。
0 0
- Python+Selenium练习篇之25-鼠标右键
- selenium-webdriver(python) -- 鼠标事件(双击,右键)
- selenium python 鼠标事件 右键、双击、拖动、鼠标悬浮
- Python selenium的webdriver之鼠标悬停
- Python+Selenium练习篇之7-利用name定位元素
- Python+Selenium练习篇之8-利用css定位元素
- Python+Selenium练习篇之9-清除文本方法
- Python+Selenium练习篇之10-刷新当前页面
- Python+Selenium练习篇之12-获取浏览器的版本号
- Python+Selenium练习篇之17-点击复选框-Checkbox
- Python+Selenium练习篇之18-自定义浏览器窗口大小
- Python+Selenium练习篇之19-断言页面标题
- Python+Selenium练习篇之22-获取页面元素大小
- Python+Selenium练习篇之23-组合键-全选文字
- Python+Selenium练习篇之26-执行JavaScript
- Python+Selenium练习篇之27-多窗口之间切换
- Python+Selenium练习篇之28-处理iframe切换
- Python+Selenium练习篇之29-处理Alert弹窗
- 记录几个非常实用的js函数
- Property or field 'siteUrl' cannot be found on object of type 'xx'
- java在oj中的输入
- 用户登录记住密码案例
- 基于hunt工具的TCP会话劫持攻击实验
- Python+Selenium练习篇之25-鼠标右键
- 史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
- 调整队形(某客某易模拟题)
- Linux进程描述符task_struct结构体详解
- 《Training:Building a Simple User Interface》
- c/c++整理--字符串(1)
- apache jsp的路径问题
- 考核和技术人员的职业规划
- java对象头