zoukankan      html  css  js  c++  java
  • appium---模拟点击事件

      在做自动化的过程中都会遇到一些无法定位到的地方,或者通过元素怎么都定位不成功的地方,这个时候我们可以使用必杀技,通过坐标定位。具体的怎么操作呢? 

    swipe点击事件

    前面安静写过一篇关于swipe的滑动app页面的,其实swipe也可以模拟点击事件,只要我们把后面的响应时间变小,然后坐标变成同一个坐标。详情swipe的用法可以参考appium---App页面滑动

    通过工具查看到这个登录/注册按钮坐标为[390,831][522,873],算得出来大概坐标为[470,850]

     话不多说直接上代码,通过swipe进行模拟点击(注册/登录)按钮

    # coding:utf-8
    from appium import webdriver
    import time
    desired_caps = {
                     'platformName': 'Android',  # 测试版本
                     'deviceName': 'emulator-5554',   # 设备名
                     'platformVersion': '5.1.1', # 系统版本
                    "appPackage": "com.taobao.taobao",   # app包名
                    "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                    "noReset": True,  # 不清空数据
                            }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    # 淘宝加载慢,加个延迟。
    time.sleep(16)
    x1 = int(470)
    y1 = int(850)
    driver.swipe(x1,y1,x1,y1,500)

    一个方法可以多种用法,但是python怎么会这样对待我们呢?当然有比这更好的方法

    tap

    tap方法表示点击事件,通过坐标的方式进行点击,一般用于元素难于定位的时候

    源码

    def tap(self, positions, duration=None):
            """Taps on an particular place with up to five fingers, holding for a certain time
          # 用五指轻敲一个特定的地方,保持一定的时间
            :Args:
             - positions - an array of tuples representing the x/y coordinates of
            # 表示x/y坐标的元组数组
             the fingers to tap. Length can be up to five.
             - duration - (optional) length of time to tap, in ms
         # 持续时间单位毫秒
            :Usage:
                driver.tap([(100, 20), (100, 60), (100, 100)], 500)
            """

    这里我们可以直接通过右侧bonds属性上坐标直接定位,不用我们在一个个计算

    # coding:utf-8
    from appium import webdriver
    import time
    desired_caps = {
                     'platformName': 'Android',  # 测试版本
                     'deviceName': 'emulator-5554',   # 设备名
                     'platformVersion': '5.1.1', # 系统版本
                    "appPackage": "com.taobao.taobao",   # app包名
                    "appActivity": "com.taobao.tao.welcome.Welcome",   # 启动launch Activity
                    "noReset": True,  # 不清空数据
                            }
    driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
    time.sleep(16)
    # 通过tap模拟点击
    driver.tap([(390,831),(522,873)],500)

    注意:更换手机跑脚本的时候,我们一定要更换坐标,每个手机的坐标可能都不同。

    其实方法还有很多种,这里就先不一个个介绍了,持续更新中~~~

  • 相关阅读:
    [VC++入门]C++中常用的运算符及微软自定义类型
    搜索引擎蜘蛛爬虫原理
    Enterprise Library 5.0
    Installshield 12 中文系列教程之 定义安装必要条件
    installshield脚本
    c# 事物处理
    InStallShield网络资源参考
    Could not execute query against OLE DB provider 'OraOLEDB.Oracle'
    frameset小结
    最痛心的距离
  • 原文地址:https://www.cnblogs.com/qican/p/11612644.html
Copyright © 2011-2022 走看看