抓取屏幕文字的例子
import uiautomator2 as u2 # 连接手机 d = u2.connect() print(d.info) def printAll(): for i,v in enumerate(d.xpath('//*').all()): if v.text!='': print("【{0:0=4}】{1}".format(i,v.text)) def printTextviewAll(): for i,v in enumerate(d.xpath('//android.widget.TextView').all()): print("【{0:0=4}】{1}".format(i,v.text)) printAll() # printTextviewAll()
uiautomator2在github上有 快速开始指南 推荐搭配web-editor快速抓取控件信息
还是很好懂的,抽出几分钟看一遍基本就可以写东西了,比如简单的
d.click(x,y) # 点击坐标
d.xpath(xp).click() # 点击控件
d.xpath(xp).wait(timeout=3) # 等待控件
d.press('back') # 按键/返回
至于xpath的写法,最常用的大概是这句了xp = "//*[re:match(@text, '^正则语句')]"
,用正则匹配查找对应文本的控件。
除了xpath还支持Uiselector的写法, 二者稍有不同, 以下是我对比测试两种写法的对比作为参考, 实际写的时候我还是用Uiselector比较多一些因为写起来整洁
UiSelector | xpath | 备注 |
---|---|---|
d(text='立即开户') 或d(description='立即开户') |
d.xpath("立即开户") |
text或description等于立即开户的元素 |
d(textMatches='正则语句') |
d.xpath("//*[re:match(@text, '^正则语句')]") |
正则 |
d(text='文本') |
d.xpath('//*[@text="文本"]') |
text |
d(textContains='文本') |
%知道% 或 //*[contains(text(), '知道')] | 包含文本text |
d(textStartsWith='文本') |
知道%或'//*[starts-with(text(), '知道')] | 包含文本开头的text |
d(description='文本') |
d.xpath('//*[@content-desc="文本"]') |
description |
d(descriptionContains='文本') |
d.xpath('//*[contains(@content-desc,"文本")]') |
包含文本description |
d(descriptionStartsWith='文本') |
d.xpath('//*[starts-with(@content-desc,"文本")]') |
包含文本开头description |
d(resourceId='文本') |
d.xpath('//*[@resource-id="文本"]') |
resourceId |
d(text='') |
d.xpath('//*[@text=""]').all() |
如果存在多个, xpath需要通过all()返回列表,列表为空返回[] |
d(text='').info |
d.xpath('//*[@text=""]').info |
获取info |
d(text='').bounds() |
d.xpath('//*[@text=""]').bounds |
获取bounds坐标 |
d(text='').center() |
d.xpath('//*[@text=""]').center() |
获取center中心点坐标 |
d(text='').count | 统计同对象数量 | |
详细见python\lib\site-packages\uiautomator2\_selector.py原代码
有了通过文本正则查找控件、获取文本、点击、返回等等,剩下的就是将这些动作组合循环,实现规律性抓取了,具体怎么写结合需要自行组合实现。
转载摘录之https://blog.csdn.net/watfe/article/details/104885546,以上给我启发很大,特别是可以定位正则语句。
比如用d(textMatches='分数.+').info 可以提取包含‘分数’开始的元素。
公共方法 |
|
---|---|
UiSelector |
checkable(boolean val) 设置搜索条件以匹配可检查的小部件。 |
UiSelector |
checked(boolean val) 设置搜索条件以匹配当前选中的小部件(通常用于复选框)。 |
UiSelector |
childSelector(UiSelector selector) 向此选择器添加子 UiSelector 条件。 |
UiSelector |
className(String className) 设置搜索条件以匹配小部件的类属性(例如,“android.widget.Button”)。 |
UiSelector |
className(Class<T> type) 设置搜索条件以匹配小部件的类属性(例如,“android.widget.Button”)。 |
UiSelector |
classNameMatches(String regex) 使用正则表达式设置搜索条件以匹配小部件的类属性。 |
UiSelector |
clickable(boolean val) 设置搜索条件以匹配可点击的小部件。 |
UiSelector |
description(String desc) 设置搜索条件以匹配小部件的内容描述属性。 |
UiSelector |
descriptionContains(String desc) 设置搜索条件以匹配小部件的内容描述属性。 |
UiSelector |
descriptionMatches(String regex) 设置搜索条件以匹配小部件的内容描述属性。 |
UiSelector |
descriptionStartsWith(String desc) 设置搜索条件以匹配小部件的内容描述属性。 |
UiSelector |
enabled(boolean val) 设置搜索条件以匹配已启用的小部件。 |
UiSelector |
focusable(boolean val) 设置搜索条件以匹配可聚焦的小部件。 |
UiSelector |
focused(boolean val) 设置搜索条件以匹配具有焦点的小部件。 |
UiSelector |
fromParent(UiSelector selector) 将子 UiSelector 条件添加到此选择器,用于从父小部件开始搜索。 |
UiSelector |
index(int index) 设置搜索条件以通过其在布局层次结构中的节点索引来匹配小部件。 |
UiSelector |
instance(int instance) 将搜索条件设置为按实例编号匹配小部件。 |
UiSelector |
longClickable(boolean val) 设置搜索条件以匹配可长时间单击的小部件。 |
UiSelector |
packageName(String name) 设置搜索条件以匹配包含小部件的应用程序的包名称。 |
UiSelector |
packageNameMatches(String regex) 设置搜索条件以匹配包含小部件的应用程序的包名称。 |
UiSelector |
resourceId(String id) 设置搜索条件以匹配给定的资源 ID。 |
UiSelector |
resourceIdMatches(String regex) 使用正则表达式设置搜索条件以匹配小部件的资源 ID。 |
UiSelector |
scrollable(boolean val) 设置搜索条件以匹配可滚动的小部件。 |
UiSelector |
selected(boolean val) 设置搜索条件以匹配当前选择的小部件。 |
UiSelector |
text(String text) 设置搜索条件以匹配小部件中显示的可见文本(例如,启动应用程序的文本标签)。 |
UiSelector |
textContains(String text) 设置搜索条件以匹配小部件中的可见文本,其中可见文本必须包含输入参数中的字符串。 |
UiSelector |
textMatches(String regex) 使用正则表达式设置搜索条件以匹配布局元素中显示的可见文本。 |
UiSelector |
textStartsWith(String text) 设置搜索条件以匹配以 text 参数为前缀的小部件中的可见文本。 |
String |
toString() |
UIAutomator2 常用命令整理
https://www.cnblogs.com/hancece/p/12912487.html
UiSelector中使用instance与index方法定位控件的区别
https://www.cnblogs.com/lovexiaov/p/uiauto_lovexiaov.html
UiSelector
https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector#instance(int)