appium+python自动化26-模拟手势点击坐标(tap)

# 前言:
有时候定位元素的时候,你使出了十八班武艺还是定位不到,怎么办呢?(面试经常会问)
那就拿出绝招:点元素所在位置的坐标

tap用法

1.tap是模拟手指点击,一般页面上元素
的语法有两个参数,第一个是positions,是list类型最多五个点,duration是持续时间,单位毫秒

tap(self, positions, duration=None):

    Taps on an particular place with up to five fingers, holding for a certain time
    
    模拟手指点击(最多五个手指),可设置按住时间长度(毫秒)
    
    :Args:
    
    - positions - list类型,里面对象是元组,最多五个。如:[(100, 20), (100, 60)] - duration - 持续时间,单位毫秒,如:500 :Usage: driver.tap([(100, 20), (100, 60), (100, 100)], 500)

坐标定位

1.如下图定位"去看看"这个按钮的坐标,可以看到右侧bonds属性:[374,831][654,906]

2.点右上角"搜索"按钮,查看bonds属性:[615,52][690,146]

参考案例

# coding:utf-8
from appium import webdriver from time import sleep desired_caps = { 'platformName': 'Android', 'deviceName': '127.0.0.1:62001', 'platformVersion': '4.4.2', 'appPackage': 'com.baidu.yuedu', 'appActivity': 'com.baidu.yuedu.splash.SplashActivity' } driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) sleep(5) # 点弹出框去看看 driver.tap([(374, 831), (654, 906)], 500) # 返回上一页 driver.back() sleep(2) # 点右上角搜素按钮 driver.tap([(615, 52), (690, 146)], 500)

弊端

通过坐标定位是元素定位的下下下策,实在没办法才用这个,另外如果换了手机分辨率,这个坐标就不能写死了,得算出所在屏幕的比例。

猜你喜欢

转载自www.cnblogs.com/jason89/p/9027416.html