如何使用appium和ruby在android中获取元素的content desc属性

如何使用appium和ruby在android中获取元素的content desc属性,android,ruby,appium,Android,Ruby,Appium,我正在尝试使用获取元素的content desc属性 @driver.find_element(:xpath, "//android.widget.TextView[@text='Automation']").attribute("content-desc") 但是得到这个错误 [debug][MJSONWP]使用args调用AppiumDriver.getAttribute(): [“xpath”、“19”、“f0ea41e6-dfee-4b9b-b7

我正在尝试使用获取元素的content desc属性

@driver.find_element(:xpath, "//android.widget.TextView[@text='Automation']").attribute("content-desc")
但是得到这个错误

[debug][MJSONWP]使用args调用AppiumDriver.getAttribute(): [“xpath”、“19”、“f0ea41e6-dfee-4b9b-b756-0b204863d0d4”]

[debug][AndroidBootstrap]向android发送命令: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“content desc”,“elementId”:“19”}

[AndroidBootstrap][BOOTSTRAP LOG][debug]已从客户端获取数据: {“cmd”:“action”,“action”:“element:getAttribute”,“params”:{“attribute”:“content desc”,“elementId”:“19”}

[AndroidBootstrap][BOOTSTRAP LOG][debug]获取了类型为ACTION的命令

[AndroidBootstrap][BOOTSTRAP LOG][debug]获得命令操作: getAttribute [AndroidBootstrap][BOOTSTRAP LOG][debug]正在返回 结果:{“状态”:7,“值”:“此元素没有 “内容描述”属性“}

我已经和appium UI检查过content desc属性是否存在,我的代码中是否有任何错误?有什么替代方法吗?

在python中(我想Ruby也会类似),我是这样做的:

Python代码

webElement = driver.find_element(By.XPATH, "//android.widget.TextView[@text='Automation']")
content-desc = webElement.get_attribute("content-desc")
print(content-desc)
此webElement.get_属性(attr)适用于@text、@checked、@enabled、@selected、@name、@value

但无论如何。。。您在未找到元素的位置发布了此消息:

[AndroidBootstrap][BOOTSTRAP LOG][debug]返回结果:{“status”:7,“value”:“此元素没有'content desc'属性”}

因此,可能您的XPATH不正确,并且您选择了错误的元素。。。
如果这对您不起作用,您必须发布Appium Inspector的图像才能帮助您尝试使用getAttribute(“contentDescription”)

我相信您的代码是正确的。可能的错误可能是xpath或属性名称不正确,因此我建议使用
contentDescription
而不是content-desc。如果不起作用,请尝试使用其他定位器