在作自动化的过程当中都会遇到一些没法定位到的地方,或者经过元素怎么都定位不成功的地方,这个时候咱们可使用必杀技,经过坐标定位。具体的怎么操做呢? html
前面安静写过一篇关于swipe的滑动app页面的,其实swipe也能够模拟点击事件,只要咱们把后面的响应时间变小,而后坐标变成同一个坐标。详情swipe的用法能够参考appium---App页面滑动python
经过工具查看到这个登陆/注册按钮坐标为[390,831][522,873],算得出来大概坐标为[470,850]web
话很少说直接上代码,经过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怎么会这样对待咱们呢?固然有比这更好的方法app
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属性上坐标直接定位,不用咱们在一个个计算spa
# 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)
注意:更换手机跑脚本的时候,咱们必定要更换坐标,每一个手机的坐标可能都不一样。3d
其实方法还有不少种,这里就先不一个个介绍了,持续更新中~~~code