Search code examples
xmlandroid-layoutlistviewxpathappium

Unable to locate element using xpath in Appium


I am unable to identify an element using xpath. Attaching the screen shot of the same. I want to identify the list of elements with the tag <android.widget.LinearLayout> under <android.widget.ListView> and I have used the XPath expression:

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

This returns all the elements only from the 1st node and not the elements under <android.widget.LinearLayout>.

Can some body help me out? Thanks in advance.

enter image description here


Solution

  • To recursively search all elements under each of the <android.widget.LinearLayout> you want this as your xpath:

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

    But be careful! This is a VERY heavy operation. In your screenshot, it looks that each one of your cells has atleast 9-10 elements per row! Asking Appium for all of those elements will kill your performance.

    I suggest doing something like:

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

    Seriously though, I warn you against doing the first method. You should ask yourself if you only want the links on the page, or text, etc etc, and then use the second xpath finding method.

    Also, read the xpath documentation for more information