【问题标题】:How to get content-desc in Android app using appium如何使用 appium 在 Android 应用程序中获取 content-desc
【发布时间】:2021-08-21 12:55:10
【问题描述】:

我正在为 Android 应用编写自动化测试。

当我试图获取文章标题的内容描述时。我无法获取它。

仅供参考,我没有文字。

List<WebElement> arrayOfProperties2 = driver.findElementsByClassName("android.view.View");

for (WebElement property : arrayOfProperties2){
    System.out.println("property" + property.getClass());

    String contentDesc = property.getAttribute("name");
    String contentDesc1 = property.getAttribute("className");
    String contentDesc2 = property.getAttribute("resourceId");
    System.out.println("contentDesc" + contentDesc);
    System.out.println("contentDesc1" + contentDesc1);
    System.out.println("contentDesc2" + contentDesc2);
}

结果:-

contentDesc
contentDesc1android.view.View
contentDesc2

【问题讨论】:

  • 我假设 getAttribute("content-desc") 和 getAttribute("contentDesc") 不起作用(它不应该但是......)。我希望有一种方法可以检索所有属性:)
  • @kiedysktos 你能告诉我它是怎么做的吗?
  • String contentDesc = property.getAttribute("content-desc");
  • @kiedysktos 它对我不起作用,并说找不到元素。
  • 那我就不知道了……

标签: java android appium


【解决方案1】:

试试这个

WebElement webElement = driver.findElement(By.xpath("//android.view.View[@content-desc='Click to Resize']"));
webElement.click();

【讨论】:

    【解决方案2】:

    将它与 UI automator2 一起使用:

    element.getAttribute(“contentDescription”)
    

    【讨论】:

      【解决方案3】:

      关注 appium 文档http://appium.io/docs/en/commands/element/find-elements/

      使用findElementByAccessibilityIdfindElementsByAccessibilityId 你会得到:

      辅助功能 ID

      读取 UI 元素的唯一标识符。对于 XCUITest,它是元素的 accessibility-id 属性。对于 Android,它是元素的 content-desc 属性。

      【讨论】:

      • 我不会同意这些……而 XML 布局缺少资源 ID。我的意思是,如果不是以一种不会通过代码审查的方式生成 XML,它会很容易处理。
      猜你喜欢
      • 2018-06-04
      • 2014-11-20
      • 2017-04-06
      • 2020-03-26
      • 2020-10-15
      • 2020-02-19
      • 2019-01-26
      • 1970-01-01
      • 2015-01-09
      相关资源
      最近更新 更多