1.安装adb
网上找一个或者FQ后官网下,然后配置环境变量即可
C:\Users\lenovo>adb version Android Debug Bridge version 1.0.39 Revision 3db08f2c6889-android Installed as D:\work\auto\adb\adb.exe
2.安装python-uiautomator2包
pip install uiautomator2
3.连接手机
3.1 用模拟器模拟手机直接启动即可
3.2 连接自己手机需要通过数据线初始化
# 手机开发调试开启,下面命令会自动安装服务到手机 python -m uiautomator2 init
C:\Users\lenovo>python -m uiautomator2 init [I 210304 13:34:43 init:155] uiautomator2 version: 2.13.0 [I 210304 13:34:43 init:352] Install minicap, minitouch [D 210304 13:34:43 init:62] Download https://tool.appetizer.io/openatx/stf-binaries/raw/0.2.2/node_modules/minitouch-prebuilt-beta/prebuilt/x86/bin/minitouch minitouch |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 33.6 KB/33.6 KB [?25h[D 210304 13:34:43 init:232] Push to /data/local/tmp/minitouch:0755 [I 210304 13:34:43 init:356] abi:x86 seems to be android emulator, skip install minicap [D 210304 13:34:43 init:250] apk-debug package-info: None [D 210304 13:34:43 init:251] apk-debug-test package-info: None [I 210304 13:34:43 init:367] Install com.github.uiautomator, com.github.uiautomator.test 2.3.1 [D 210304 13:34:43 init:162] Shell: ('pm', 'uninstall', 'com.github.uiautomator') [D 210304 13:34:44 init:162] Shell: ('pm', 'uninstall', 'com.github.uiautomator.test') [D 210304 13:34:45 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator.apk app-uiautomator.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 2.1 MB/2.1 MB [?25h[D 210304 13:34:49 init:232] Push to /data/local/tmp/app-uiautomator.apk:0644 [D 210304 13:34:49 init:162] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator.apk') [I 210304 13:34:51 init:332] - app-uiautomator.apk installed [D 210304 13:34:51 init:62] Download https://tool.appetizer.io/openatx/android-uiautomator-server/releases/download/2.3.1/app-uiautomator-test.apk app-uiautomator-test.apk |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 1.2 MB/1.2 MB [?25h[D 210304 13:34:54 init:232] Push to /data/local/tmp/app-uiautomator-test.apk:0644 [D 210304 13:34:54 init:162] Shell: ('pm', 'install', '-r', '-t', '/data/local/tmp/app-uiautomator-test.apk') [I 210304 13:34:55 init:332] - app-uiautomator-test.apk installed [I 210304 13:34:55 init:340] Install atx-agent 0.9.5 [D 210304 13:34:55 init:62] Download https://tool.appetizer.io/openatx/atx-agent/releases/download/0.9.5/atx-agent_0.9.5_linux_386.tar.gz atx-agent_0.9.5_linux_386.tar.gz |⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿| 4.2 MB/4.2 MB [?25h[D 210304 13:35:07 init:232] Push to /data/local/tmp/atx-agent:0755 [D 210304 13:35:08 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--stop') [D 210304 13:35:08 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d') [D 210304 13:35:08 init:162] Shell: ('/data/local/tmp/atx-agent', 'server', '--nouia', '-d') [I 210304 13:35:08 init:377] Check atx-agent version [D 210304 13:35:08 init:390] Forward: local:tcp:64449 -> remote:tcp:7912 [D 210304 13:35:09 init:393] atx-agent version 0.9.5 Successfully init AdbDevice(serial=127.0.0.1:62028)
4.代码确认
import uiautomator2 as ut d=ut.connect('127.0.0.1:62028') print(d.device_info)
5.调试
#手机打开应用后,通过这个命令可以只是应用名称 C:\Users\lenovo>uiautomator2 current { "package": "com.eg.android.AlipayGphone", "activity": "com.eg.android.AlipayGphone.AlipayLogin" } #自动打开应用 d.app_start("com.eg.android.AlipayGphone")
https://github.com/openatx/uiautomator2/blob/master/QUICK_REFERENCE.md标签:13,uiautomator,Python,34,apk,init,写安卓,uiautomator2,210304 From: https://www.cnblogs.com/zyf531/p/17140916.html