Katalon Studio - xapth of same element differs from mobile to mobile

why does the xpath of same element differs from device to device. Is it Katalon studio or apium drawback. My test are failing on another device as Katalon studio is unable to find the xpath from device 1 on which the recording was done.

HOW TO SOLVE THIS PROBLEM ?

DEVICE #1 XPATH…
//hierarchy/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.FrameLayout[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.view.ViewGroup[1]/android.widget.ImageView[1]

DEVICE #2 XPATH…

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

1 Like

Any possible solution?

I don’t know why there is a difference but using such long, absolute xpaths is not going to be very robust. Newer versions of Katalon might give you better identifiers but I ended up using Appium Inspector to find relative xpaths last time I tested, with 7.2.5. If you have Devs that can give elements a useful ID that would be even better.

1 Like

I agree with Dan. It is a better practice to write relative xpaths as they are more flexible , easier to understand for other people , shorter and easier to maintain.
Try to use unique atributes of the element for example: //[@resource-id=‘atributeValueOfYourElement’] . Another example: //[@text=‘Login’] . you can lear more here: https://www.guru99.com/xpath-selenium.html

below are the only attributes for this element in problem.

Question is not abt going and asking the developer to follow best practise, the question how to handle it in katalon if it occurs?

tried using regex as well, but no luck.

//hierarchy/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.widget(.*)android.view.ViewGroup[1]/android.widget.ImageView[1]