python控制手机
使用的是UIautomator2和weditor,配合上酷Q机器人进行qq回复
初始化
手机连接电脑,打开开发者模式,打开usb调试,文件传输模式,然后命令行输入以下
adb devives
确认设备id号
手机安装控制组件,确认授权调试:
python -m uiautomator2 init
打开辅助编辑器
python -m weditor
常用命令:
import uiautomator2 as u2 d = u2.connect("--serial-here--") # 只有一个设备也可以省略参数 d = u2.connect() # 一个设备时 d = u2.connect("10.1.2.3") # 通过设备的IP连接(需要在同一局域网且设备上的atx-agent已经安装并启动) d.app_current() # 获取前台应用 packageName, activity d.app_start("com.example.app") # 启动应用 d.app_start("com.example.app", stop=True) # 启动应用前停止应用 d.app_stop("com.example.app") # 停止应用 app = d.session("com.example.app") # 启动应用并获取session # session的用途是操作的同时监控应用是否闪退,当闪退时操作,会抛出SessionBrokenError app.click(10, 20) # 坐标点击 # 无session状态下操作 d.click(10, 20) # 坐标点击 d.swipe(10, 20, 80, 90) # 从(10, 20)滑动到(80, 90) d.swipe_ext("right") # 整个屏幕右滑动 d.swipe_ext("right", scale=0.9) # 屏幕右滑,滑动距离为屏幕宽度的90% d.press("back") # 模拟点击返回键 d.press("home") # 模拟Home键 d.send_keys("hello world") # 模拟输入,需要光标已经在输入框中才可以 d.clear_text() # 清空输入框 # 执行shell命令 output, exit_code = d.shell("ps -A", timeout=60) # 执行shell命令,获取输出和exitCode output = d.shell("pwd").output # 这样也可以 exit_code = d.shell("pwd").exit_code # 这样也可以 # 元素操作 d.xpath("立即开户").wait() # 等待元素,最长等10s(默认) d.xpath("立即开户").wait(timeout=10) # 修改默认等待时间 # 常用配置 d.settings['wait_timeout'] = 20 # 控件查找默认等待时间(默认20s) # xpath操作 d.xpath("立即开户").click() # 包含查找等待+点击操作,匹配text或者description等于立即开户的按钮 d.xpath("//*[@text='私人FM']/../android.widget.ImageView").click() d.xpath('//*[@text="私人FM"]').get().info # 获取控件信息 for el in d.xpath('//android.widget.EditText').all(): print("rect:", el.rect) # output tuple: (left_x, top_y, width, height) print("bounds:", el.bounds) # output tuple: (left, top, right, bottom) print("center:", el.center()) el.click() # click operation print(el.elem) # 输出lxml解析出来的Node # 监控弹窗(在线程中监控) d.watcher.when("跳过").click() d.watcher.start()
watcher的用法:
https://blog.****.net/VeastLee/article/details/87743018
uiautomator2 使用Python测试 Android应用
http://www.cnblogs.com/fnng/p/8486863.html
UIautomator2的api
https://blog.****.net/Eugene_3972/article/details/76629066
https://blog.****.net/ricky_yangrui/article/details/81415365
https://github.com/openatx/uiautomator2/blob/master/QUICK_REFERENCE.md
adb无法连接,计算机拒绝问题解决
https://blog.****.net/u013250071/article/details/80527993