【问题标题】:Unable to locate element using xpath in Appium无法在 Appium 中使用 xpath 定位元素
【发布时间】:2014-07-27 07:22:30
【问题描述】:

我无法使用 xpath 识别元素。附上相同的屏幕截图。我想在<android.widget.ListView> 下识别带有标签<android.widget.LinearLayout> 的元素列表,并且我使用了XPath 表达式:

//android.widget.ListView/android.widget.LinearLayout

这只会返回 1st 节点中的所有元素,而不是 <android.widget.LinearLayout> 下的元素。

有人能帮我吗?提前致谢。

【问题讨论】:

    标签: xml android-layout listview xpath appium


    【解决方案1】:

    你可以试试:

    WebElement row = driver.findElementByXPath("//android.widget.ListView[1]/android.widget.LinearLayout[1]");
    

    【讨论】:

      【解决方案2】:

      要递归搜索每个 <android.widget.LinearLayout> 下的所有元素,您希望将其作为 xpath:

      xpath = '//android.widget.ListView/android.widget.LinearLayout/*'
      

      但要小心!这是一个非常繁重的操作。在您的屏幕截图中,您的每个单元格每行至少有 9-10 个元素!向 Appium 询问所有这些元素会影响你的表现。

      我建议这样做:

      xpath = '//android.widget.ListView/android.widget.LinearLayout//class_you_want'
      

      说真的,我警告您不要使用第一种方法。你应该问问自己你是否只想要页面上的链接,或者文本等,然后使用第二种xpath查找方法。

      Also, read the xpath documentation for more information

      【讨论】:

      • 非常感谢。将尝试解决方案并随时通知您。 :)
      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2017-05-07
      • 2021-02-11
      • 2016-07-01
      • 1970-01-01
      • 2021-08-01
      • 2017-01-20
      • 2021-03-27
      相关资源
      最近更新 更多