uiautomator2 - Python 操控 Android 的自动化库(openatx)

uiautomator2 - Python 操控 Android 的自动化库(openatx)


设备端HTTP服务+Python客户端,pip装完一行connect直接操控手机:XPath定位、App管理、Watcher弹窗处理,8.4k Stars

uiautomator2 是 openatx 出品的 Android 自动化 Python 库——在电脑上用 Python 直接操控 Android 设备,简单、易用、稳定,是移动端自动化测试和爬虫脚本的老牌标杆。

工作原理

设备端跑一个基于 UiAutomator 的 HTTP 服务(jar 也开源),Python 客户端通过 HTTP 调用设备能力——把 Android 自动化能力通过 HTTP 接口暴露给 Python,写起来非常直观。

核心能力

  • Python 直连设备:u2.connect() 一行连上,支持 USB 序列号、WiFi 直连、多设备管理
  • 丰富选择器:resource-id / text / className / XPath 定位控件,点击、滑动、输入、拖拽
  • App 管理:安装、启动、停止、卸载、清除数据
  • 屏幕操作:截图、录屏、层级 dump、手势
  • 设备信息:分辨率、当前 Activity、输入法、剪贴板等
  • Watcher 机制:注册回调自动处理弹窗等打断场景
  • 配套工具:uiautodev 元素检查器(npx uiautodev -open 浏览器里看控件树)

生态(openatx 全家桶)

  • adbutils:adb 纯 Python 封装
  • weditor:元素层级可视化编辑
  • uiautodev:新一代元素检查工具

快速上手

pip install uiautomator2
import uiautomator2 as u2
d = u2.connect()
d.app_start('tv.danmaku.bili', stop=True)
d.xpath('//*[@text="我的"]').click()

项目数据

  • GitHub 8.4k Stars,最新版 v3.7.0(3.x 重构,2.x 用户建议看 2to3 迁移文档)
  • Android 4.4+ / Python 3.8+,开源协议
  • QQ 群 815453846,有活跃中文社区
12400举报0子龙•1个月前
点击获取 ^_^

暂无评论