Files
aiData/Doc/2、Python + uiautomator2.md

79 lines
1.8 KiB
Markdown
Raw Permalink Normal View History

2026-01-12 07:49:18 +08:00
文档
https://zhuanlan.zhihu.com/p/19790598010
```cmd
pip install -U uiautomator2
pip install uiautodev
pip install pillow
```
**启动**
```
uiauto.dev
```
**探秘工作原理,知其然更知其所以然**
python - uiautomator2主要由两部分组成Python客户端和移动设备。
1. **Python端**负责运行我们编写的脚本然后向移动设备发送HTTP请求。
2. **移动设备**运行着封装了uiautomator2的HTTP服务收到请求后解析请求内容并把它转化成uiautomator2的代码。
整个工作流程如下先在移动设备上安装atx - agent它是个守护进程安装好后atx - agent会启动uiautomator2服务默认7912端口开始监听。然后在PC上编写测试脚本并执行这个过程就相当于向移动设备的server端发送HTTP请求。最后移动设备通过WIFI或USB接收到请求执行相应的操作。
```
# 通过包名启动
d.app_start("com.tencent.mm") # 启动微信
```
https://zhuanlan.zhihu.com/p/25181382631
```
# 等待元素出现最多10秒间隔1秒
d(text="登录").wait(timeout=10, interval=1.0)
d(text="登录").click()
# 等待元素消失
d(text="加载中...").wait_gone(timeout=10)
```
https://uiauto.dev/
```
adb kill-server
adb start-server
```
https://github.com/appium/appium-uiautomator2-server/releases
关闭华为手机鸿蒙3的纯净模式
```
adb shell pm disable-user com.huawei.security.privacycenter
adb shell pm disable-user com.huawei.appmarket
```
```
adb install -r appium-uiautomator2-server-v9.9.1.apk
adb shell am instrument -w -e debug false io.appium.uiautomator2.server/.ServerInstrument
adb shell am broadcast -a uiautomator.dump
adb pull /sdcard/window_dump.xml .
```
adb uiautomator dump /data/local/tmp/ui.xml