You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python的uiautomator2读取Android应用视图的Content Description

解决uiautomator2读取Android视图Content Description的问题

错误原因

你代码里用的contentDesc不是uiautomator2选择器允许的参数,正确的参数名是description(也可以简写为desc),这就是触发报错的直接原因。

修正元素查找代码

把contentDesc='New Room'替换成description='New Room'或者desc='New Room',修正后的元素查找代码如下:

# 定位目标元素
target_element = amc(
    resourceId="com.company.le.mesh.meshapp:id/toolbar_layout",
    className="android.widget.FrameLayout",
    description='New Room'
)
# 检查元素是否存在
print(target_element.exists)

读取元素的Content Description值

如果要获取元素的Content Description内容,先定位到元素,再通过.info属性获取元素的详细信息字典,从中提取description字段即可:

if target_element.exists:
    # 获取元素的完整信息字典
    element_info = target_element.info
    # 提取Content Description内容
    content_desc = element_info.get('description')
    print(f"Content Description内容:{content_desc}")

完整示例代码

import uiautomator2 as u2
import subprocess 

# 获取设备序列号并建立连接
adb_device = subprocess.run('adb devices', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
device_serial = adb_device.stdout.split('\n')[1][:-7]
amc = u2.connect(device_serial) 

# 定位目标元素
target_element = amc(
    resourceId="com.company.le.mesh.meshapp:id/toolbar_layout",
    className="android.widget.FrameLayout",
    description='New Room'
)

# 检查元素存在并读取Content Description
if target_element.exists:
    element_info = target_element.info
    content_desc = element_info.get('description')
    print(f"获取到的Content Description: {content_desc}")
else:
    print("未找到目标元素")

内容的提问来源于stack exchange,提问作者Avinash Kumar Jha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 07:45:24