Need to identify xpath for android element using appium

前端 未结 4 2697
没有蜡笔的小新
没有蜡笔的小新 2021-02-20 16:49

I am testing my Android application using Appium framework. I have an android screen that doesn\'t have ids for its views (and I don\'t want to add...), so I thought using Xpath

相关标签:
4条回答
  • 2021-02-20 17:11

    Right now there's a couple nasty bugs with XPath on android that explain the behaviors you're seeing. They are scheduled to be fixed in the 1.3.1 release

    1. You can't search by root nodes.. link
      • Unfortunately, this means that a verbose xpath is likely the xpath(pun) to success
    2. You sometimes get 240 of an element when you only have 16 link

    Ideally, you could look for the resource-id of the android.widget.LinearLayout parent of all 16 RelativeLayouts and then do something like:

    //android.widget.LinearLayout[@resource-id="foo"]/android.widget.RelativeLayout
    

    Your verbose solution did not work because you gave one of the layouts a position of [2].

    Here it is, fixed:

    //android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ViewAnimator[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout/android.widget.RelativeLayout
    

    .... please, please use the first solution.

    I'm looking into a solution for your problem using UiAutomator locator strategy, but no answers yet (because .fromChild() and .fromParent() seem to be broken)

    0 讨论(0)
  • 2021-02-20 17:18

    Just try this u can access easily

    driver.findElement(By.xpath("//*[@class='android.widget.FrameLayout' and @bounds='[418,564][780,885]']")).click();
    

    Screenshot of UI

    0 讨论(0)
  • 2021-02-20 17:30

    The following code worked for me:

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

    Courtesy: Unable to locate element using xpath in Appium

    0 讨论(0)
  • 2021-02-20 17:34

    Keeping in mind the fact that //foo[1] returns all foo nodes which are the first children of a their parent (not the first child of the foo node) try something like this:

    //ScrollView/LinearLayout/LinearLayout[2]/LinearLayout/RelativeLayout

    Not tested, but it should give you all the RelativeLayouts which are a child of a LinearLayout which is a child of a LinearLayout which is the second child of a LinearLayout which is the child of a ScrollView.

    0 讨论(0)
提交回复
热议问题