Selenium中ActionChains讲解

这篇具有很好参考价值的文章主要介绍了Selenium中ActionChains讲解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

        简介:

ActionChains是模拟鼠标的一些操作

web自动化中的常用操作:

driver.click():元素点击

driver.send_keys():输入

driver.swipe(start_x, start_y, end_x, end_y):根据坐标滑动(其实swipe的源码就是用的ActionChains)

而在APP自动化中,往往可能会有连续的操作,这时就需要用到ActionChains

Python中引入库方法:

#引入方式一
from selenium.webdriver.common.action_chains import ActionChains

#引入方式二
from selenium.webdriver import ActionChains

ActionChains方法列表

click(on_element=None) ——单击鼠标左键

click_and_hold(on_element=None) ——点击鼠标左键,不松开

context_click(on_element=None) ——点击鼠标右键

double_click(on_element=None) ——双击鼠标左键

drag_and_drop(source, target) ——拖拽到某个元素然后松开

drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开

key_down(value, element=None) ——按下某个键盘上的键

key_up(value, element=None) ——松开某个键

move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标

move_to_element(to_element) ——鼠标移动到某个元素

move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置

perform() ——执行链中的所有动作(每次要执行ActionChains时都要加perform才会执行

release(on_element=None) ——在某个元素位置松开鼠标左键

send_keys(*keys_to_send) ——发送某个键到当前焦点的元素

send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

swipe的源码解析

def swipe(self: T, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> T:
     # 创建手指touch(ActionChains默认是模拟鼠标操作mouse)  
     touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")
     
     # 实例化ActionChains
     actions = ActionChains(self)
     
     # 将mouse换成touch
     actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
     # 按下
     actions.w3c_actions.pointer_action.pointer_down()
     # 滑动时间
     if duration > 0:
         actions.w3c_actions = ActionBuilder(self, mouse=touch_input, duration=duration)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
     # 抬起
     actions.w3c_actions.pointer_action.release()
     # 执行
     actions.perform()
     return self

其实我们可以在此基础上进行添加修改,在抬起前面加多个移动就可以实现连续多点滑动的操作

def swipe(self, x0、y0、x1、y1、x2、y2)

     # 创建手指touch(ActionChains默认是模拟鼠标操作mouse)  
     touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")
     
     # 实例化ActionChains
     actions = ActionChains(self)
     
     # 将mouse换成touch
     actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(x0, y0)
     # 按下
     actions.w3c_actions.pointer_action.pointer_down()
     
     # 移动到某坐标点
     actions.w3c_actions.pointer_action.move_to_location(x1, y1)
     actions.w3c_actions.pointer_action.move_to_location(x2, y2)
     # 抬起
     actions.w3c_actions.pointer_action.release()
     # 执行
     actions.perform()
     
     传几个点就只能移动几次。

当然也可以把 "抬起" 与前面的"移动"代码拆分开进行单独的封装,代码如下:

    def new_touch(self, x, y):     # 按下第一个点
        touch_input = PointerInput(interaction.POINTER_TOUCH, "touch")

        actions = ActionChains(self.driver)
        actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
        actions.w3c_actions.pointer_action.move_to_location(x, y)
        actions.w3c_actions.pointer_action.pointer_down()
        

    def swipe_touch(self, x, y):   # 移动到某点,可以多次执行即可完成多点滑动

        actions.w3c_actions.pointer_action.pause(0)  #
        actions.w3c_actions.pointer_action.move_to_location(x, y)


    def perform_touch(self):     # 抬起并执行
        
        actions.w3c_actions.pointer_action.release()  # 抬起
        actions.perform()  # 执行动作
        

还有一种是不用w3c标准的,直接用ActionChains中的方法文章来源地址https://www.toymoban.com/news/detail-700727.html

ac = ActionChains(Android)
    ac.move_to_element(el1)
    ac.click_and_hold()
    ac.move_to_element(el2)
    ac.move_to_element(el3)
    ac.move_to_element(el4)
    ac.move_to_element(el5)
    ac.move_to_element(el6)
    ac.move_to_element(el7)
    ac.move_to_element(el8)
    ac.move_to_element(el9)
    ac.release()
    ac.perform()

到了这里,关于Selenium中ActionChains讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 数十位高级测工联合讲解Selenium自动化测试框架工作原理

    一、Selenium是什么? 用官网的一句话来讲:Selenium automates browsers. That\\\'s it!简单来讲,Selenium是一个用于Web应用程序自动化测试工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作浏览器一样。支持的浏览器包括IE,Firefox,Safari,Chrome等。Selenium 不仅仅是一个工具或

    2024年02月05日
    浏览(30)
  • Selenium ActionChains

    目录 Actions接口 Action构造器 暂停 释放所有Actions 键盘操作 按键 按下按键

    2024年02月12日
    浏览(15)
  • Python + Selenium —— ActionChains动作链!

    当你需要执行复杂的操作时,比如将一个元素按住拖动到另一个元素上去,需要移动鼠标然后点击并按下键盘某个按键等等。 当然,在 Web 页面上,这种操作好像比较少。 但是,如果遇到了怎么办呢?这就需要用到 ActionChains 这个类啦。 ActionChains 提供了对动作的链式操作,

    2024年01月23日
    浏览(28)
  • Selenium基础 — Selenium自动化测试框架介绍

    Selenium是一个用于Web应用程序测试的工具。 只要在测试用例中把预期的用户行为与结果都描述出来,我们就得到了一个可以自动化运行的功能测试套件。 Selenium测试套件直接运行在浏览器中,就像真正的用户在操作浏览器一样。 Selenium也是一款同样使用Apache License 2.0协议发布

    2024年02月02日
    浏览(43)
  • Selenium自动化测试之Selenium IDE

    学习路线指引(点击解锁) 知识定位 人群定位 🧡 Python实战微信订餐小程序 🧡 进阶级 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 💛Python量化交易实战💛 入门级 手把手带你打造一个易扩展、更安全、效率更高的量

    2024年02月10日
    浏览(134)
  • 什么是Selenium?使用Selenium进行自动化测试

    什么是 Selenium?   Selenium 是一种开源工具,用于在 Web 浏览器上执行自动化测试(使用任何 Web 浏览器进行 Web 应用程序测试)。   等等,先别激动,让我再次重申一下,Selenium 仅可以测试Web应用程序。我们既不能使用 Selenium 测试任何桌面(软件)应用程序,也不能测试

    2024年02月13日
    浏览(29)
  • JavaScript+Selenium自动化测试_selenium和js能一起做自动化测试

    var webdriver = require(‘selenium-webdriver’), By = webdriver.By, until = webdriver.until; var driver = new webdriver.Builder() .forBrowser(‘chrome’) .build(); driver.get(‘https://www.baidu.com’); driver.findElement(By.id(‘kw’)).sendKeys(‘webdriver’); driver.findElement(By.id(‘su’)).click(); driver.wait(until.titleIs(‘webdriver_百度

    2024年04月25日
    浏览(31)
  • 什么是Selenium?如何使用Selenium进行自动化测试?

      Selenium 是一种开源工具,用于在 Web 浏览器上执行自动化测试(使用任何 Web 浏览器进行 Web 应用程序测试)。   等等,先别激动,让我再次重申一下,Selenium 仅可以测试Web应用程序。我们既不能使用 Selenium 测试任何桌面(软件)应用程序,也不能测试任何移动应用程

    2024年02月02日
    浏览(31)
  • JavaScript+Selenium自动化测试_selenium javascript版

    先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7 深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前! 因此收集整理了一份《2024年最新软件测试全套学习资料》

    2024年04月26日
    浏览(47)
  • python+selenium自动化测试环境搭建步骤(selenium环境搭建)

     📢专注于分享软件测试干货内容,欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📢交流讨论:欢迎加入我们一起学习! 📢资源分享:耗时200+小时精选的「软件测试」资料包 📢 软件测试学习教程推荐:火遍全网的《软件测试》教程 ​ 1.自动化测试概念:       是

    2024年01月22日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包