ActionChains和TouchAction可以用来模拟点击、双击、滑动等事件。ActionChains用于执行PC端的鼠标移动、按键、拖拽等事件;TouchActions用法与ActionChains类似,可以用来模拟PC和移动端的点击、滑动、拖拽等手势操作。

ActionChains和TouchAction都是将动作存储在队列中,然后执行perform()方法,按队列顺序执行动作。

ActionChains

有两种执行方式

链式:

ActionChains(driver).move_to_element(element).click(element).perform()

分布式:

actions=ActionChains(driver)
actions.move_to_element(element)
actions.click(element)
actions.perform()

例一:点击,右键,双击操作

测试页面:http://sahitest.com/demo/clicks.htm

python代码:

self.driver.get("http://sahitest.com/demo/clicks.htm")
click = self.driver.find_element_by_xpath("//*[@value='click me']")
doubleclick = self.driver.find_element_by_xpath("//*[@value='dbl click me']")
rightclick = self.driver.find_element_by_xpath("//*[@value='right click me']")
action= ActionChains(self.driver)
action.click(element_click)
action.context_click(element_rightclick)
action.double_click(element_doubleclick)
action. perform()

例二:移动鼠标到某个元素上

将鼠标移动到“新闻”后进行点击

python代码:

self.driver.get("http://www.baidu.com")
ele = self.driver.find_element_by_link_text("新闻")
action = ActionChains(self.driver)
action.move_to_element(ele)
action.click()
action.perform()

例三:通过像素坐标点击页面

使用move_by_offset()方法实现点击页面,像素坐标可以使用截图工具来获取。

python代码:

ActionChains(self.driver).move_by_offset(x, y).click().perform() #左键点击
ActionChains(self.driver).move_by_offset(x, y).context_click().perform() #右键点击

例四:模拟键盘输入

模拟键盘输入可以使用win32api模块,也可以用 selenium的WebElement对象的send_keys()方法来实现:

element = self.driver.find_element_by_id(element) element.send_keys(**"test"**) element.send_keys(Keys.BACK_SPACE) assert element.get_attribute("value") == "tes"

ActionChains类也可以模拟键盘输入:

Action = ActionChains(driver) action.send_keys(Keys.BACK_SPACE) # 回退
action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL) # CTRL+A
action.perform() # 执行

测试页面:http://sahitest.com/demo/label.htm

在文本框1中输入内容,然后将文本框1的内容复制粘贴到文本框2

self.driver.get("http://sahitest.com/demo/label.htm")
ele1 = self.driver.find_element_by_xpath("/htmL/body/label[1]/input")
ele2 = self.driver.find_element_by_xpath("/html/body/label[2]/table/tbody/tr/td[2]/input")
ele1.click()
action= ActionChains(self.driver)
action.send_keys("testing").pause(1)
action.send_keys(Keys.SPACE).pause(1) # 空格
action.send_keys("1").pause(1)
action.send_keys(Keys.BACK_SPACE) #回退
action.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL) #CTRL+A
action.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL) #CTRL+C
action.key_down(Keys.CONTROL,ele2).send_keys('v').key_up(Keys.CONTROL) #CTRL+V
action.send_keys(Keys.BACK_SPACE).perform()

例五:拖拽

测试页面:http://sahitest.com/demo/dragDropMooTools.htm

python代码

self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
drag_ele = self.driver.find_element_by_id("dragger")
Item1 = self.driver.find_element_by_xpath("/htmL/body/div[2]")
Item2 = self.driver.find_element_by_xpath("/html/body/div[3]")
Item3 = self.driver.find_element_by_xpath("/html/body/div[4]")
action= ActionChains(self.driver)
action.drag_and_drop(drag_ele, Item1).pause(1) # 方法1
action.click_and_hold(drag_ele).release(Item2).pause(1)# 方法2
action.click_and_hold(drag_ele).move_to_element(Item3).release()# 方法3
action.perform()

TouchAction

ActionChains无法操作H5页面,TouchAction可以对H5页面进行操作,实现点击,滑动,拖拽,模拟手势等各种操作。

手势控制方法

  • double_tap 双击

  • flick 滑动

  • flick_element 从某个元素位置开始滑动

  • long_press 长按

  • move 手势移动指定偏移

  • Perform 执行

  • release 释放手势

  • scroll 点击并滚动

  • scroll_from_element 从某个元素位置开始手势点击并滚动(向下滑动为负数,向上滑动为正数)

  • flick_element——从某个元素位置开始手势滑动(负数:向上滑动,正数:向下滑动)

  • tap 在指定元素上点击

  • tap_and_hold 在指定元素上点击但不释放

例一:点击、滑动

百度搜索关键字,tap方法点击百度一下,滑动到底部,点击下一页

python代码:

self.driver.get("http://www.baidu.com")
input = self.driver.find_element_by_id("kw")
search = self.driver.find_element_by_id("su")
input.send_keys("test")
action = TouchActions(self.driver)
action.tap(search)
action.perform()
action.scroll_from_element(input, 0, 10000).perform()
next = self.driver.find_element_by_link_text("下一页 >")
next.click()

总结

ActionChains和TouchAction实现了鼠标和键盘的模拟输入,更详细的介绍可以参考官方文档 https://selenium-python.readthedocs.io/api.html

--THE END--

文章标题:Selenium ActionChains、TouchAction方法

本文作者:hiyo

本文链接:https://hiyong.gitee.io/posts/selenium-actionchains-touchaction/

欢迎关注公众号:「测试开发小记」及时接收最新技术文章!

Selenium ActionChains、TouchAction方法的更多相关文章

  1. Java&Selenium智能等待方法封装

    Java&Selenium智能等待方法封装 ExpectedConditions方法还有很多,自然也可以继续扩展很多 package util; import org.openqa.selen ...

  2. Java&Selenium 模拟键盘方法封装

    Java&Selenium 模拟键盘方法封装 package util; import java.awt.AWTException; import java.awt.Robot; import ...

  3. Java&Selenium控制滚动条方法封装

    Java&Selenium控制滚动条方法封装 package util; import org.openqa.selenium.JavascriptExecutor; import org.o ...

  4. Java&Selenium 模拟鼠标方法封装

    Java&Selenium 模拟鼠标方法封装 package util; import org.openqa.selenium.By; import org.openqa.selenium.W ...

  5. selenium的定位方法-单元素定位

    selenium自动化测试中,提供了单个元素定位方法,多个元素定位方法,2种方式都是根据元素属性:ID.NAME.CLASS_NAME.TAG_NAME.CSS_SELECTOR.XPATH.LINK ...

  6. Selenium -- ActionChains().move_by_offset() 卡顿的解决方法

    测试运行时间 运行时间 发现每次0.5秒,此时需要修改默认的时间 打开Python安装目录下的Lib\site-packages\selenium\webdriver\common\actions\p ...

  7. selenium定位元素方法汇总

    #打开网页前三步 from selenium import webdriver driver=webidriver.Chrome() driver.get("https://www.baid ...

  8. selenium采用xpath方法识别页面元素

    有些HTML页面中的元素中属性较少,经常有找不到id.class.name等常用属性的时候,这个时候xpath.css就能很好的识别到我们的元素. Firefox和chrome浏览器中均有xpath. ...

  9. selenium各种定位方法(转)

    selenium使用 Xpath CSS JavaScript jQuery的定位方法 (治疗selenium各种定位不到,点击不了的并发症) 2017年07月28日 22:47:36 阅读数:369 ...

随机推荐

  1. 三. Vue组件化

    1. 认识组件化 1.1 什么是组件化 人面对复杂问题的处理方式 任何一个人处理信息的逻辑能力都是有限的,所以当面对一个非常复杂的问题时我们不太可能一次性搞定一大堆的内容. 但是我们人有一种天生的能力 ...

  2. Kubernetes-20:日志聚合分析系统—Loki的搭建与使用

    日志聚合分析系统--Loki 什么是Loki? Loki 是 Grafana Labs 团队最新的开源项目,是一个水平可扩展,高可用性,多租户的日志聚合系统.它的设计非常经济高效且易于操作,因为它不会 ...

  3. CSP2020复赛游记

    CSP2020复赛游记 由于本蒟蒻侥幸通过PJ和TG的分数线并且侥幸的拿了一等,所以侥幸的来参加复赛 11.04~11.05 期中考,挂 11.06 对答案,炸 11.07 开始了第一次CSP复赛 坐 ...

  4. 【2014广州市选day1】JZOJ2020年9月12日提高B组T4 字符串距离

    [2014广州市选day1]JZOJ2020年9月12日提高B组T4 字符串距离 题目 Description 给出两个由小写字母组成的字符串 X 和Y ,我们需要算出两个字符串的距离,定义如下: 1 ...

  5. 使用 Jasypt 加密 Spring Boot 配置文件

    一.添加依赖包 <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId> ...

  6. python核心高级学习总结5--------python实现线程

    在代码实现上,线程的实现与进程的实现很类似,创建对象的格式都差不多,然后执行的时候都是用到start()方法,与进程的区别是进程是资源分配和调度的基本单位,而线程是CPU调度和分派的基本单位.其中多线 ...

  7. 第9.10节 Python中IO模块其他文件操作属性和方法简介

    本文中所有案例中的fp都是使用open函数打开文件返回的一个文件对象,为了节省篇幅,大部分没有提供文件打开的代码. 一. 文件是否关闭的属性 属性名:closed 功用:判断文件是否关闭 示例: &g ...

  8. mysql中table schema的基本操作

    我们通常对数据库进行的增删插检操作,是针对数据库中的文件.mysql数据库中还有一些表(是view,只能做select操作)记录了现有表的meta data,比如某个column的名字,它的定义是什么 ...

  9. 项目使用RQ队列的思考

    碎遮项目的后端异步处理经历了 无处理->多线程/多进程->celery异步队列->RQ队列 的调整和修改,先简单说明一下为什么会存在这样的过程. 在nmap的使用指南中,提到过这样的 ...

  10. flask加载配置文件的三种方法

    1.第一种方法也是我们最长用到的,包括我们项目中也是采用第一种的方法,加载配置文件 配置信息全部写在config.py里面,在主app.py的文件中写入 import config app.confi ...