Python+Selenium学习--鼠标事件
场景
前景讲解了鼠标的click()事件,而我们在实际的web产品测试中,有关鼠标的操作,不仅仅只有单击,有时候还包括右击,双击,拖动等操作,这些操作包含在ActionChains类中。
ActionChains类鼠标操作的常用方法:
- context_click() 右击
- double_click() 双击
- drag_and_drop() 拖动
- move_to_element() 鼠标悬停在一个元素上
- click_and_hold() 点击鼠标左键,不松开
鼠标事件
1.鼠标右击事件
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 鼠标事件.py
@time: 2018-09-21 14:14
@desc:
'''
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time driver = webdriver.Firefox() driver.get('https://www.baidu.com/') #定位需要右击元素
right = driver.find_element_by_id("kw")
#对定位对象进行右击操作
ActionChains(driver).context_click(right).perform() time.sleep(2)
driver.quit()
ActionChains用于生成用户的行为;所有的行为都存储在actionchains对象,通过perform()执行存储的行为
perform()执行所有ActionChains存储的行为,perform()同样也是ActionChains类提供的方法,通常他们配合使用
2.鼠标双击事件
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 鼠标事件.py
@time: 2018-09-21 14:14
@desc:
'''
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time driver = webdriver.Firefox() driver.get('https://www.baidu.com/') #定位需要双击元素
double = driver.find_element_by_id("kw")
#对定位对象进行双击击操作
ActionChains(driver).double_click(double).perform() time.sleep(2)
driver.quit()
对于操作系统的操作来说,双击使用比较频繁,带对于web而言,比较少
3.鼠标拖放操作
drag_and_drop(source,target)
在源元素上按下鼠标,然后移动到目标元素释放
source:鼠标按下的源元素
target:鼠标释放的目的元素
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 鼠标事件.py
@time: 2018-09-21 14:14
@desc:
'''
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time driver = webdriver.Firefox() driver.get('https://www.baidu.com/') #定位源元素
source = driver.find_element_by_id("xx")
#定位目的元素
target = driver.find_element_by_id("xx")
#执行移动操作
ActionChains(driver).drag_and_drop(source,target).perform() time.sleep(2)
driver.quit()
4.移动鼠标到元素上
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 鼠标事件.py
@time: 2018-09-21 14:14
@desc:
'''
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time driver = webdriver.Firefox() driver.get('https://www.baidu.com/') #定位元素
above = driver.find_element_by_id("xx")
#执行移动操作
ActionChains(driver).move_to_element(above).perform() time.sleep(2)
driver.quit()
5.按下鼠标左键不松开
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 鼠标事件.py
@time: 2018-09-21 14:14
@desc:
'''
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
import time driver = webdriver.Firefox() driver.get('https://www.baidu.com/') #定位元素
aa=driver.find_element_by_id("su")
#执行点击元素不松开
ActionChains(driver).click_and_hold(aa).perform() time.sleep(2)
driver.quit()
Python+Selenium学习--鼠标事件的更多相关文章
- Python+Selenium学习--键盘事件
场景 我们在实际的测试工作中,有时候需要使用tab键将焦点移动到下一个元素,用于验证元素的排序是否正确.webdriver的Keys()类提供键盘上所有的操作,甚至可以模拟一些组合键的操作,如Ctrl ...
- 【python+selenium学习】Python常见错误之:IndentationError: unexpected indent
初入python+selenium学习之路,总会遇到这样那样的问题.IndentationError: unexpected indent,这个坑我已经踏进数次了,索性记录下来.都知道Python对代 ...
- Python自动化学习--鼠标和键盘事件
from selenium import webdriver from selenium.webdriver import ActionChains import time driver = webd ...
- selenium-webdriver(python) (十五) -- 鼠标事件
本节重点: ActionChains 类 context_click() 右击 double_click() 双击 drag_and_drop() 拖动 测试的产品中有一个操作是右键点击文件列 ...
- selenium的鼠标事件操作
自动化测试过程中,经常会用到鼠标事件,在selenium的action_chains模块的ActionChains定义了鼠标操作的一些事件,要使用ActionChains类中的方法,首先需要对Acti ...
- Python+Selenium学习--自动化测试模型
前言 一个自动化测试框架就是一个集成体系,在这一体系中包含测试功能的函数库.测试数据源.测试对象识别标准,以及种可重用的模块.自动化测试框架在发展的过程中经历了几个阶段,模块驱动测试.数据驱动测试.对 ...
- jQuery学习-鼠标事件
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Python+Selenium学习笔记15 - 读取txt和csv文件
读取txt的内容并用百度查找搜索 1 # coding = utf-8 2 3 from selenium import webdriver 4 import time 5 6 # 打开浏览器 7 d ...
- Python+Selenium学习--下拉框处理
场景 下拉框也是web 页面上非常常见的功能,webdriver 对于一般的下拉框处理起来也相当简单,要想定位下拉框中的内容,首先需要定位到下拉框:这样的二次定位 下拉框一般有以下两种方式: 鼠标移上 ...
随机推荐
- java中的byte
8 bit (位) = 1 Byte (字节) java中的byte就是Byte 1024 Byte = 1KB 1024 KB = 1MB 1:“字节”是byte,“位”是bit : 2: 1 by ...
- 上下两个div, 一个固定高度, 另一个铺满屏幕
<div class="box"> <div class="el1"></div> <div class=" ...
- Leetcode 题解 Trapping Rain Water
题目链接:https://leetcode.com/problems/trapping-rain-water/description/ 思路: 1.先找到最左侧第一个高度不为0的柱子i. 2.从i+1 ...
- 尚硅谷springboot学习19-日志切换
查看相关依赖关系,排除相关依赖,引入新的日志依赖 slf4j+log4j的方式: <dependency> <groupId>org.springframework.boot& ...
- Linux grep命令使用方法
Linux系统中grep命令可以根据指定的字符串或者正则表达式对文件内容进行匹配查找.在Linux文件处理和SHELL编程中使用广泛. grep基本语法 用法: grep [选项] "字符串 ...
- 关于openwrt使用web升级提示固件版本不对的处理方法
参考资料:https://blog.csdn.net/caoshunxin01/article/details/79355602 当openwrt使用web升级提示固件版本不对: The upload ...
- 【原】wow64 x86/x64 代码切换过程分析
下面以ntdll32!ZwQueryInformationProcess API为例分析 x86代码与x64代码之间的切换过程, 32bit的test程序: step1: ntdll32!ZwQuer ...
- 13.BeanUtils组件-基础.md
目录 用途 基本属性的设置 Map数据的拷贝 对象的拷贝 转换器 用途 可以用来对JavaBean的各种增强操作 基本属性的设置 package per.liyue.code.beanutildemo ...
- IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化:
IT蓝豹强烈推荐:符合1-2年工作经验,开发中的难点及相关优化: IT蓝豹 ------------------> sqlite数据库版本升级 1.sqlite升级步骤: 1.自己写一个类继承自 ...
- monkeyrunner学习笔记
前面部分内容转自http://blog.csdn.net/zm2714/article/details/7980634 Android自动化测试之Monkeyrunner使用方法及实例 目前andro ...