Search code examples
javaappiumappium-android

How to get text attribute from coordinates?


I'm trying to get the text form a specific element. The page has many TextViews. All the text elements have the same resource-id and classname. So driver.findElement()and all the googled ways don't work here.

The only way to copy the text attribute from that item is to use X,Y coordinates.

Is there any way to get text form specific coordinates?


Solution

  • I found a way to it. We can use the full hierarchy of the element. Easily by using / between parents and children

    like this:

    driver.findElement(By.xpath("//android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.support.v4.widget.i[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[2]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.support.v4.view.ViewPager[1]/android.view.ViewGroup[1]/android.widget.RelativeLayout[1]/android.widget.FrameLayout[1]/android.support.v7.widget.RecyclerView[1]/android.support.v7.widget.RecyclerView[1]/android.widget.LinearLayout[2]/android.widget.TextView[2]")).getText();
    

    UiAutomator2 doesn't show this XPATH attribute. I used Katalon studio - Mobile Spy for finding the complete Xpath hierarchy