Search code examples
javaandroidtestingxpathappium

Android XPath index issue


Very new to Android Testing.
I came across this Xpath

xpath: (//android.widget.ImageView[1])[3]

I know that in ImageView1 "1" is the index of the element.
What is [3] here???

Update:

I am running a test (mobile App). The above XPath is to tap on the Bell icon.
It did tap on the bell icon perfectly.
But today when I run my test it doesn't tap on the Bell icon instead it taps on the user Profile Picture.

enter image description here

Using appium I found the xpath of the Bell icon as below:

xpath: /hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[1]/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup/android.view.ViewGroup[2]/android.view.ViewGroup[1]/android.view.ViewGroup[2]/android.view.ViewGroup/android.widget.ImageView

Now when I use this new xpath the Bell icon is clicked.

Are both the Xpaths same??
Why is this new xpath so long? is there a way to shorten this??
Any kind of help is really Appreciated!!!


Solution

  • You are getting the absolute xpath using the appium(which you have stated above) and the one you are using like //android.widget.ImageView[1])[3] is the relative xpath of the icon.

    Absolute xpath : It is the path from the root element to the particular element. Relative xpath : It is the reference path of the element or particular path to the element.