python_api操作之swipe和九宫格
一。appium常用API操作
异常处理:uiautomator init failed;can't find uiautomator
建议:先关闭第三方辅助工具,如果还不行执行以下命令:
appuim 启动异常。
解决方案(wind):
1.进入手机:adb shell
2.查看运行的服务:pm list package | grep uiautomator
3.删除服务:adb uninstall 服务名称 (例如:io.appium.uiautomator2.server)
1.API操作:
- driver.start_activity() 跳转到指定的页面
- swipe(), 滑动
-例子:
屏幕,宽 800, 高 900
790, 450, 10, 450
driver.swipe(790, 450, 10, 450)
问题在于我传的坐标到底应该是多少。
取决于屏幕的宽度
先获取屏幕宽度 width, height
向左滑,我的起始点 x 坐标, width * 0.9, height * 0.5 结束点 x 坐标, 0.1, height *0.5
结束点的 height 和 起始点保持一致。
# 获取屏幕的宽度。
size = driver.get_window_size()
# 得到一个字典
width = size["width"]
height = size["height"]
# 向左滑动
# 这个就是滑动的标准操作
driver.swipe(
start_x= width * 0.9,
start_y= height * 0.5,
end_x= width * 0.1,
end_y= height * 0.5
)
- 触屏操作
- 多指操作 (放大,缩小)
- 键盘输入()
2.app元素定位(建议元素使用的先后顺序):
- ID
- content-desc ===> driver.find_element_by_accessibility_id()
- uiautomator ===> driver.find_element_by_android_uiautomator()
- xpath
3.TouchAction触碰动作(封装了常用操作,通过链式调用进行使用):
- action = TouchAction()
- action.press().wait().move_to().wait().move_to().realease().perform()
- 链式调用
按压:
press()
等待
wait()
移动
move_to()
释放
realease()
4.九宫格操作:
- press 九宫格的第一个格子, 坐标确认。
- wait 停顿
- move_to 移动到第二个
- wait
- move_to 移动到第三个
- wait
- move_to 移动到第四个
- wait
- release() 释放
- perform() 释放操作
思路:
1.先拿到起始点的位置信息
2.剧上图可知,宽,高被分为了6等份(画的有偏差请忽略)
3.计算出 九个点的位置
初始x坐标位置=start_x
初始y坐标位置=start_y
第一排左数第一个位置:x坐标=start_x+1/6 * x ;y坐标=start_y+1/6 * y
第一排左数第二个位置:x坐标=start_x+3/6 * x ;y坐标=start_y+1/6 * y
第一排左数第三个位置:x坐标=start_x+5/6 * x ;y坐标=start_y+1/6 * y
第二排左数第一个位置:x坐标=start_x+1/6 * x ;y坐标=start_y+3/6 * y
第二排左数第一个位置:x坐标=start_x+3/6* x ;y坐标=start_y+3/6 * y
第二排左数第一个位置:x坐标=start_x+5/6 * x ;y坐标=start_y+3/6 * y
第三排左数第一个位置:x坐标=start_x+1/6 * x ;y坐标=start_y+5/6 * y
第三排左数第一个位置:x坐标=start_x+3/6 * x ;y坐标=start_y+5/6 * y
第三排左数第一个位置:x坐标=start_x+5/6 * x ;y坐标=start_y+5/6 * y
4.实现(待续):