ERROR: could not get idle state

执行uiautomator dump页面xml命令时,动态UI无法捕获

执行dump命令 adb -s device_serias shell uiautomator dump /sdcard/demo_app_2024-01-18_20-51-49_1.xml 失败,提示“ERROR: could not get idle state.”

可以使用直接截图 然后回传orc识别获取

adb shell screencap -p /sdcard/screen.png
adb pull /sdcard/screen.png

方法是用appium 来替代shell uiautomator dump抓取层级结构并生成 xml图,不仅不报错,而且每次标签很全,appium支持终端 和docker部署

appium 实际是通过 uiautomator2 来抓 xml 的。 如果只用 dump xml,那可以直接用 uiautomator2,示例如下:

import uiautomator2 as u2
import time

# 连接到设备
device = u2.connect()

# 获取当前界面的 XML
xml = device.dump_hierarchy()

# 将 XML 保存到文件
with open('screen_dump.xml', 'w', encoding='utf-8') as f:
    f.write(xml)