1. 简介
uiautomator2
是一个 Android UI自动化测试的 Python 包,允许控制和测试 Android 设备它是基于 Google 的 uiautomator
框架的二次开发。
2. 安装和环境配置
需要配置 Python 环境和安装 uiautomator2 第三方库:
首先安装 Python 的环境
此处省略安装步骤
Python 解释器
建议使用 3.11 版本
使用命令来查看Python 版本
python --version
安装命令
pip install uiautomator2
初始化设备
python -m uiautomator2 init
3. 基础使用教程
导入uiautomator2 库
import uiautomator2 as u2
连接设备
device = u2.connect_usb('7b068a46')
点击操作
device.click(x, y)
输入文本
device.send_keys("text")
滚动查找
device(scrollable=True).scroll.to(text="Settings")
4. 高级功能
XPath
element = device.xpath('//android.widget.TextView[@text="Settings"]').get()
控件组合查询
element = device(text="Settings", className="android.widget.TextView")
滚动
device(scrollable=True).scroll.to(text="phone")