Mobile Custom XPATH locator

Hi I have a question does xpath in mobile & web works the same way? In Web i can use xpath like following-sibling but does it work the same way for mobile ? Cause i tried it seems to not be working ??

This is the custom xpath that i made //*[@class = ‘android.widget.TextView’ and (@text = ‘User ID’ or . = ‘User ID’)]/following-sibling::android.widget.ViewGroup/descendant::android.widget.EditText


Was it the wrong syntax??

1 Like

Hi there, :wave:

Thank you very much for your topic! It may take a little while before Katalon team member or others forum members respond to you.

In the meantime, you can double-check your post to see if you can add any extra information i.e. error logs, HTML codes, screenshots, etc. Check out this posting guide to help us help you better!

Thanks! :sunglasses:
Katalon Community team

I might try some variation on your pathway, since you have several ‘siblings’. So, how about?

//*[@class = 'android.widget.TextView' and (@text = 'User ID' or . = 'User ID')]/following-sibling::android.widget.ViewGroup[1]/android.widget.EditText

or maybe even

//*[@text = 'User ID' or . = 'User ID']/following-sibling::android.widget.ViewGroup[1]/android.widget.EditText

I haven’t been able to get the follow-sibling to work on mobile but I believe you can use this custom xpath to do the same thing:

//*[@class = ‘android.widget.TextView’ and (@text = ‘User ID’ or . = ‘User ID’)]/..//*[@class = 'android.widget.EditText']

The bit in bold below - I can’t see that anywhere in the hierarchy, so syntax might be correct but one of your nodes is not:

“This is the custom xpath that i made //*[@class = ‘android.widget.TextView’ and (@text = ‘User ID’ or . = ‘User ID’)]/following-sibling::android.widget.ViewGroup/descendant::android.widget.EditText”

That’s a classic challenge with mobile automation, especially when the resource IDs are dynamic or non-existent. Usually, when standard XPaths aren’t cutting it, switching to a more robust strategy like contains() or indexing can help stabilize your tests.

If you haven’t tried it yet, using //android.widget.TextView[contains(@text, ‘YourText’)] is often much more reliable than an absolute path. Also, if you’re on Appium/Katalon, sometimes checking the UI Automator Viewer or the Appium Inspector can reveal “Content-desc” labels that are way easier to target than a long string of nested classes.

yeah agree contains() and content-desc can help

but in this case the issue isn’t really the XPath style, it’s the hierarchy. In mobile, elements that look like siblings often aren’t actually siblings in the XML, so following-sibling just won’t work

better to go up to parent then down again, something like

//*[@text='User ID']/..//android.widget.EditText

or even better, use resource-id / content-desc if available, way more stable than XPath