Appium中scroll和drag_and_drop根据元素位置滑动


Posted in Python onFebruary 15, 2022

背景

我们在操作APP应用时,有些需要从一个元素滑动到另外一个元素时,这时候我们无法确定坐标,所以swipe 根据坐标滑动方式就无法使用了,如下图:从 课堂直播 上滑到 直播公开课 位置

Appium中scroll和drag_and_drop根据元素位置滑动


这时候我们就需要使用其他滑动方式,我们想到可以根据元素进行滑动,Appium 里面根据元素来进行滑动的方式主要方法为 scrolldrag_and_drop

scroll 介绍

说明

从一个元素滚动到另一个元素,只能是两个元素之间的滑动。

方法详情

def scroll(self: T, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> T:
        """Scrolls from one element to another

        Args:
            origin_el: the element from which to being scrolling
            destination_el: the element to scroll to
            duration: a duration after pressing originalEl and move the element to destinationEl.
                Default is 600 ms for W3C spec. Zero for MJSONWP.

        Usage:
            driver.scroll(el1, el2)

        Returns:
            Union['WebDriver', 'ActionHelpers']: Self instance
        """

        # XCUITest x W3C spec has no duration by default in server side
        if self.w3c and duration is None:
            duration = 600

        action = TouchAction(self)
        if duration is None:
            action.press(origin_el).move_to(destination_el).release().perform()
        else:
            action.press(origin_el).wait(duration).move_to(destination_el).release().perform()
        return self

参数:

  • origin_el - 要滚动的起始元素
  • destination_el - 要滚动到的结束元素
  • duration - 持续时间,单位毫秒,默认为 600 ms

操作场景

  • 进入网易云首页
  • 从课堂直播滑动到直播公开课位置

关键代码实现

# 定位到课堂直播元素
el1 = driver.find_element(AppiumBy.XPATH, "//*[@text='课堂直播']").click()

# 定位到直播公开课元素
el2 = driver.find_element(AppiumBy.XPATH, "//*[@text='直播公开课']").click()

# 执⾏滑动操作
driver.scroll(el1,el2)

说明

操作过程有 惯性,需要添加duration参数,参数值越大,惯性越小。

drag_and_drop 介绍

说明

从一个元素滑动到另一个元素,第二个元素代替第一个元素原本屏幕上的位置。

方法详情

def drag_and_drop(self: T, origin_el: WebElement, destination_el: WebElement) -> T:
        """Drag the origin element to the destination element

        Args:
            origin_el: the element to drag
            destination_el: the element to drag to

        Returns:
            Union['WebDriver', 'ActionHelpers']: Self instance
        """
        action = TouchAction(self)
        action.long_press(origin_el).move_to(destination_el).release().perform()
        return self

参数:

  • origin_el - 要滑动页面的起始元素
  • destination_el - 要滑动页面到结束元素

操作场景

  • 进入网易云首页
  • 从课堂直播滑动到直播公开课位置

关键代码实现

# 定位到课堂直播元素
el1 = driver.find_element(AppiumBy.XPATH, "//*[@text='课堂直播']").click()

# 定位到直播公开课元素
el2 = driver.find_element(AppiumBy.XPATH, "//*[@text='直播公开课']").click()

# 执⾏滑动操作
driver.drag_and_drop(el1,el2)

说明

不能设置持续时间,没有惯性

滑动和拖拽使用场景选择

滑动和拖拽无非就是考虑是否具有“惯性”,以及传递的参数是“元素”还是“坐标”。

  • scroll:有 “惯性” ,传入 “元素”,可以通过设置duration参数来进行控制惯性大小
  • drag_and_drop:无 “惯性” ,传入 “元素”
  • swipe:有 “惯性” ,传入 “坐标”,可以通过设置duration参数来进行控制惯性大小

说明: 添加duration参数,参数值越大,惯性越小

到此这篇关于Appium中scroll和drag_and_drop根据元素位置滑动的文章就介绍到这了,更多相关Appium 元素滑动内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
机器学习10大经典算法详解
Dec 07 Python
python3如何将docx转换成pdf文件
Mar 23 Python
python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
Apr 01 Python
基于python解线性矩阵方程(numpy中的matrix类)
Oct 21 Python
python+Django实现防止SQL注入的办法
Oct 31 Python
Python 切分数组实例解析
Nov 07 Python
Python常用模块logging——日志输出功能(示例代码)
Nov 20 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
Mar 02 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
May 24 Python
matplotlib subplot绘制多个子图的方法示例
Jul 28 Python
浅谈Python描述数据结构之KMP篇
Sep 06 Python
详解python内置模块urllib
Sep 09 Python
python 远程执行命令的详细代码
Feb 15 #Python
python 详解turtle画爱心代码
python分分钟绘制精美地图海报
基于PyQT5制作一个桌面摸鱼工具
Feb 15 #Python
python接口测试返回数据为字典取值方式
Feb 12 #Python
Python socket如何解析HTTP请求内容
Feb 12 #Python
python全面解析接口返回数据
Feb 12 #Python
You might like
php空间不支持socket但支持curl时recaptcha的用法
2011/11/07 PHP
PHP实现图片裁剪、添加水印效果代码
2014/10/01 PHP
在TP5数据库中四个字段实现无限分类的示例
2019/10/18 PHP
JavaScript判断变量是否为undefined的两种写法区别
2013/12/04 Javascript
JS实现动态移动层及拖动浮层关闭的方法
2015/04/30 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记9)
2015/12/24 Javascript
Knockoutjs 学习系列(二)花式捆绑
2016/06/07 Javascript
详解js数组的完全随机排列算法
2016/12/16 Javascript
JavaScript 栈的详解及实例代码
2017/01/22 Javascript
layer弹出层全屏及关闭方法
2018/08/17 Javascript
Vue.js+cube-ui(Scroll组件)实现类似头条效果的横向滚动导航条
2019/06/24 Javascript
ES6常用小技巧总结【去重、交换、合并、反转、迭代、计算等】
2019/12/21 Javascript
原生javascript中this几种常见用法总结
2020/02/24 Javascript
微信小程序实现页面左右滑动
2020/11/16 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
2021/01/05 Vue.js
[01:25]DOTA2自定义游戏灵园鬼域等你踏足
2015/10/30 DOTA
python复制文件代码实现
2013/12/23 Python
Python下使用Psyco模块优化运行速度
2015/04/05 Python
今天 平安夜 Python 送你一顶圣诞帽 @微信官方
2017/12/25 Python
在pandas多重索引multiIndex中选定指定索引的行方法
2018/11/16 Python
python按行读取文件并找出其中指定字符串
2019/08/08 Python
浅谈对pytroch中torch.autograd.backward的思考
2019/12/27 Python
基于python实现查询ip地址来源
2020/06/02 Python
Python同时处理多个异常的方法
2020/07/28 Python
Python操作dict时避免出现KeyError的几种解决方法
2020/09/20 Python
Anya Hindmarch官网:奢侈设计师手袋及配饰
2018/11/15 全球购物
泰国健康和美容服务预订网站:GoWabi
2019/06/03 全球购物
开办化妆品公司创业计划书
2013/12/26 职场文书
七一表彰活动方案
2014/01/18 职场文书
护士感人事迹
2014/05/01 职场文书
领导班子作风建设年个人整改措施
2014/09/29 职场文书
会议室管理制度范本
2015/08/06 职场文书
2019年七夕情人节浪漫祝福语大全!
2019/08/08 职场文书
html form表单基础入门案例讲解
2021/07/15 HTML / CSS
一小时迅速入门Mybatis之bind与多数据源支持 Java API
2021/09/15 Javascript
ConditionalOnProperty配置swagger不生效问题及解决
2022/06/14 Java/Android