【问题标题】:Android hierarchy for XPath to interact with specific itemXPath 与特定项目交互的 Android 层次结构
【发布时间】:2014-09-06 19:22:41
【问题描述】:

我也是自动化、Android、Selenium、Appium 和 xpath 的新手。我知道这很糟糕。

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

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

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

我尝试过的方法:

  • xpath://*[contains(@android:contentDescription,'example text')]
  • 我可以通过将它们查找为 ImageView 来获得访问权限,但正如我所提到的,我需要使用自定义视图

我的代码看起来像这样:

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

感谢您的帮助!

【问题讨论】:

    标签: android xpath selenium-webdriver appium


    【解决方案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!
    

    【讨论】:

    • 谢谢你的回答,但这不是我的事它甚至使用这个(我猜)字段(content-desc 或其他)或者我应该选择其他方式来访问元素。
    • 编辑添加其他定位器策略
    • 谢谢,您的编辑帮助很大!如果其他人可能需要这个,我把它留在这里:这是关键文件:code.google.com/p/selenium/source/browse/…
    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多