抓取屏幕文字的例子
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)