在日常的测试中,经常会遇到需要鼠标去操作的一些事情,比如说悬浮菜单、拖动验证码等,这一节我们来学习如何使用webdriver模拟鼠标的操作

首页模拟鼠标的操作要首先引入ActionChains的包

from selenium.webdriver.common.action_chains import ActionChains
而对于ActionChains包,一般的写法是:

这是这个方法一般的书写格式,下面我们来看一如何使用模拟鼠标操作的具体案例

1.鼠标拖动操作(滑动验证码问题)

方法:

drag_and_drop(self, source, target)

source:鼠标拖动的原始元素

target:鼠标拖动到的另外一个元素(的位置)

拖动source元素到target元素的位置

drag_and_drop_by_offset(self, source, xoffset, yoffset)

source:鼠标拖动的原始元素

xoffset:鼠标把元素拖动到另外一个位置的x坐标

yoffset:鼠标把元素拖动到另外一个位置的y坐标

拖动source元素到指定的坐标

演示案例:

我们用淘宝的注册页面案例来说明鼠标拖动操作:把滑块从左端移到右端。

#selenium 模拟鼠标操作(ActionChains)

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

#模拟鼠标操作--拖动滑动验证码
browser=webdriver.Chrome()
browser.get('https://reg.taobao.com/member/reg/fill_mobile.htm')

#点击确定按钮(同意协议)
button1=browser.find_element_by_id('J_AgreementBtn')
button1.click()
sleep(1)
#输入手机号
'''<input autocomplete="off" class="form-text mobile-input" name="mobile" id="J_Mobile" type="text" value="" placeholder="请输入你的手机号码" data-inner_placeholder="r_p_input_inner_enterMobile" data-outer_placeholder="r_p_input_enterMobile"
data-spm-anchor-id="a2145.7268393.0.i1.f9aa5d7ckyZzB3">'''
inputs=browser.find_element_by_css_selector('.form-text.mobile-input')
inputs.send_keys('18896583137')
#获取滑动条大小
span_back=browser.find_element_by_css_selector('#nc_1__scale_text')
print(type(span_back))
span_back_size=span_back.size #size返回元素尺寸,text返回元素文本
print(span_back_size) #字典
#获取滑块位置
button2=browser.find_element_by_css_selector('#nc_1_n1z')
button2_location=button2.location
print(button2_location)

# 拖动操作:drag_and_drop_by_offset.
# 将滑块的位置由初始位置,右移一个滑动条长度(即为x坐标在滑块位置基础上,加上滑动条的长度,y坐标保持滑块的坐标位置)
x=button2_location['x']+span_back_size['width']
y=button2_location['y']
ActionChains(browser).drag_and_drop_by_offset(button2,x,y).perform()

2.鼠标悬浮操作

方法:

move_to_element (element) :鼠标移动(悬浮)到某个元素之上

element,要悬浮的元素

接下来主要对淘宝网首页的地址悬浮菜单来进行演示:


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

# 模拟鼠标操作-鼠标悬浮菜单-淘宝网首页地区选择
driver = webdriver.Chrome()
driver.get("https://www.taobao.com/")
sleep(1)

# 获取要悬浮的元素,并使用move_to_element()方法
element_list = driver.find_element_by_xpath("//*[@id='J_SiteNavBdL']/li[1]/div[1]/span[1]")
ActionChains(driver).move_to_element(element_list).perform()

# 悬浮元素出现菜单后,可以点击悬浮菜单里的元素了
driver.find_element_by_css_selector("#J_SiteNavRegionList > li:nth-child(4)").click()

3.ActionChains的其他操作:移动鼠标、右击、双击、结合键盘按键的操作等。。。
context_click(element) :

右击element元素

double_click(element):

双击element元素

move_by_offset(xoffset,yoffset):

移动鼠标到指定的x,y位置(相对于浏览器的绝对位置)

move_to_element_with_offset(element, xoffset, yoffset):

相对element元素,移动鼠标到指定的x,y位置(相对于element元素的相对位置)

click_and_hold(element1=None):

在element1元素上按下鼠标左键,并保持按下动作(元素默认为空)

release(element2=None):

在element2元素上松开鼠标左键(元素默认为空)

key_down(key , element1=None):

在element1元素上,按下指定的键盘key(ctrl、shift等)键,并保持按下动作(元素默认为空)

key_up(key , element2=None):

在element2元素上,松开指定的键盘key(元素默认为空)

send_keys(key):

向当前定位元素发送某个key键

send_keys_to_element(element ,key):

向element元素发送某个key键



模拟鼠标操作(ActionChains)(转 侵删)的更多相关文章

  1. Python+Selenium自动化 模拟鼠标操作

    Python+Selenium自动化 模拟鼠标操作   在webdriver中,鼠标的一些操作如:双击.右击.悬停.拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就 ...

  2. windows7如何用键盘模拟鼠标操作

    windows7如何用键盘模拟鼠标操作 https://jingyan.baidu.com/article/6dad5075104907a123e36e38.html 听语音 37453人看了这个视频 ...

  3. selenium模拟鼠标操作

    Selenium提供了一个类ActionChains来处理模拟鼠标事件,如单击.双击.拖动等. 基本语法: class ActionChains(object): """ ...

  4. webdriver模拟鼠标操作

    ActionChains 生成模拟用户操作的对象 from selenium.webdriver.common.action_chains import ActionChains ActionChai ...

  5. ui自动化--鼠标操作ActionChains

    需要先引入鼠标操作模块:from selenium.webdriver.common.action_chains import ActionChains 实际上ActionChains这个模块的实现的 ...

  6. C# 模拟鼠标操作

    [Flags] enum MouseEventFlag : uint //设置鼠标动作的键值 { Move = 0x0001, //发生移动 LeftDown = 0x0002, //鼠标按下左键 L ...

  7. python+selenium模拟鼠标操作

    from selenium.webdriver.common.action_chains import ActionChains #导入鼠标相关的包 ------------------------- ...

  8. python之selenium玩转鼠标操作(ActionChains)

    前提: 一般人用selenium自动化时,会用到模拟鼠标操作的情况,像单击,双击,右击,左击啊等,这个时候我们就要用到ActionChains了. 内容: 1.ActionChains用法整理 cli ...

  9. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

随机推荐

  1. 关于.netMVC 出现@ViewBag 出现错误(波浪红线)的解决方法

    解决vs2015.vs2013解决mvc5 viewbag问题 1.关闭vs2015或者vs2013 打开我的电脑或者文件夹 2.打开我的电脑 在地址栏输入 %UserProfile%\AppData ...

  2. 快速解读linq语法

    在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...

  3. 比特币PoW

    比特币区块头结构 字段 大小(Byte) 说明 nVersion 4 区块版本号,表示本区块遵守的验证规则 hashPrevBlock 32 前一区块的哈希值,使用SHA256(SHA256(父区块头 ...

  4. Oracle函数总结

    <Trunc()> 描       述(实际应用):截取小数或者日期整数 简      介:https://baike.baidu.com/item/trunc/9657216?fr=al ...

  5. Springboot配置excludePathPatterns不生效

    Springboot添加拦截器配置excludePathPatterns不生效 code: @Configurationpublic class ServiceConfig implements We ...

  6. centos8安装redis

    一,下载: 1,下载页面: https://redis.io/ 2,下载 [root@localhost source]# wget http://download.redis.io/releases ...

  7. linux(centos8):用tr替换或删除字符

    一,tr命令的用途 tr命令可以替换或删除文件中的字符 它从标准输入设备读取数据, 处理完成将结果输出到标准输出设备 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnbl ...

  8. 拿了十几个offer,怎样做选择?

    本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 最近收到好几个读者的咨询,关于如何选offer的 ...

  9. 使用阿里云镜像仓库构建国外 Docker 镜像

    使用阿里云镜像仓库下载国外镜像 在日常使用 Docker 或 K8S 的过程中,经常会需要到国外的网站中下载镜像,但是有些网站在国内是无法访问的.对于这个问题可以使用阿里云提供的镜像仓库进行下载,然后 ...

  10. Excel基础—为什么学习Excel

    吾生也有涯,而知也无涯 点赞再看,养成习惯 自从个人计算机开始普及以后,Excel就得到了广泛的传播,工作学习生活中不处不存在Excel的影子,不论是考勤,工资还是其他的统计等等,都离不开Excel. ...