XPath的Android层次结构与特定项目交互

时间:2014-07-16 09:59:36

标签: android xpath selenium-webdriver appium

我也是自动化,Android,selenium,appium和xpath的新手。我知道这很糟糕。

我为Android设备编写测试,但我必须测试的应用程序有很多的costum视图。我发现与这些自定义项交互的最佳方式是在视图中添加“ android:contentDescription ”字段。我唯一的问题是如何访问具有指定contentDescription的元素?这是特定于Android的问题,我甚至不确定content-desc是我正在寻找的字段。

我有Android UI Animator Viewer提供的层次结构:

http://i.imgur.com/NUGc56o.png

我尝试过的方式:

  • xpath:// * [contains(@android:contentDescription,'example text')]
  • 我能够通过将它们作为ImageView进行访问,但正如我所提到的,我需要使用自定义视图

我的代码看起来像这样的somtihng:

driver.findElementByXPath("//*[constains(@content-desc,'Login')]").click();

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您也可以尝试使用辅助功能标签或UIAutomator定位器策略。 这是Appium's documentation on those.

您的xpath不正确。它应该是:"//android.widget.ImageView[@content-desc='Login']"

这里有一些你应该做的伪代码:

login_image = driver.findElementByXPath("//android.widget.ImageView[@content-desc='Login']"); // Gets you the WebElement

print login_image.getClass(); // Just for debugging, make sure it's not nil/null

login_image.click(); // Click on it!