python+uiautomator2+adb

uiautomator2

基于google uiautomator的安卓UI自动化框架,支持python编写测试脚本对设备进行自动化控制。原理是在设备上运行http服务器,将http接口封装成python库

uiautomator只能使用java语言,每次都要上传到设备上运行,uiautomator2可以实现所见即所得的测试

环境搭建

1、已安装python及adb环境

2、安装uiautomator2:

python 复制代码
pip install -U uiautomator2

3、连接安卓设备:设备需开启开发者选项,adb devices确认设备已连接

4、安卓设备安装ATX并进行初始化:

python 复制代码
python -m uiautomator2 init
python -m uiautomator2 init --mirror --serial $SERIAL

初始化内容

安装minicap、minitouch、atx-agent,启动atx-agent

(负责屏幕显示、屏幕控制、监控uiautomator2的运行)

python 复制代码
[I 240130 16:49:38 init:156] uiautomator2 version: 2.16.25
[I 240130 16:49:38 init:373] Install minicap, minitouch
......
[I 240130 16:49:38 init:391] Already installed com.github.uiautomator apks
......
[I 240130 16:49:39 init:350] Check atx-agent version

5、安装weditor

python 复制代码
pip install -U weditor

6、打开weditor

python 复制代码
python -m weditor 或 weditor

7、连接设备

选择对应系统,输入设备序列号进行连接,开启实时刷新

左侧可以看到设备实时页面,中间是定位元素的信息,右侧是自动生成的代码区、代码运行、控制台日志等

问题及解决方法

atx初始化失败

python 复制代码
python -m uiautomator2 init


在浏览器打开网址,可以看到报错403,这是因为网络限制无法访问该地址

解决方法是避开内网限制:

1、设置外网()

2、设备开启USB网络共享

3、笔电可以连接可访问外网的网络

python 复制代码
python -m uiautomator2 init --mirror --serial $SERIAL
python 复制代码
# 出现以下报错,去掉--mirror就可以了
usage: __main__.py [-h] [-d] [-s SERIAL]
                   {version,init,screenshot,identify,install,uninstall,healthcheck,check,start,stop,current,doctor,console,purge}
                   ...
__main__.py: error: unrecognized arguments: --mirror

# 把$SERIAL改成adb devices查询到的序列号
......
  File "D:\Program Files (x86)\python\lib\site-packages\adbutils\_adb.py", line 134, in check_okay
    raise AdbError(self.read_string_block())
adbutils.errors.AdbError: device '$SERIAL' not found

weditor安装失败

指定版本进行安装

python 复制代码
pip install weditor==0.6.4
相关推荐
-今昭-18 分钟前
《V2V 迁移实战:将 VMware 虚拟机转为 OpenStack 可用 Glance qcow2 镜像》
开发语言·python
2601_9623815840 分钟前
【转】Python渗透测试工具:sqlmap
python·渗透测试·sql注入·sqlmap·数据库安全
朗宇芯工控1 小时前
注塑机械手自动化上下料原理、流程与应用优势
机器人·自动化·制造·工业·运动控制系统
用户3721574261351 小时前
如何使用 Python 从 Word 文档中提取图片
python
白山编程大哥3 小时前
Java 集合算法:从排序、查找到底层原理的实战指南
java·python·算法
2601_962304914 小时前
把出片接进自动化流水线:2026 年批量 AI 视频生成工具的脚本契约与同类项目对照
运维·人工智能·自动化
昭昭日月明4 小时前
RAGFlow 入门,不用从零造轮子
python·ai编程
天远数科4 小时前
零信任架构实战:基于天远身份证OCR构建自动化高并发移动支付网关
人工智能·架构·自动化·ocr
莓有烦恼吖5 小时前
Vibe Coding 的一些思考
python
小白学大数据5 小时前
超简单:用 Python 让 Excel 飞起来:用 openpyxl 把重复报表整理交给脚本
开发语言·数据库·python·excel