Files
aiData/Doc/2、Python + uiautomator2.md
HuangHai b66f683dfb 'commit'
2026-01-12 07:49:18 +08:00

79 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

文档
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