Appium returns same element with //*[contains(@text,'text')] selector

As the title says, Appium returns (not everytime, rather rarely) the same element with selectors:
//*[contains(@text,'Order Total')]/following-sibling::clazzName[@resource-id='resourceId']
Is this a bug, or just me using contains() xpath function wrong?

source.txt (55.1 KB) ?

are Subtotal and Order Total elements contained inside each other(one is a parent and the other one is a child) or they are siblings?

I think that if they have a parent-child relation so it is possible it will choose the same elements. you use the same resource-id for both after all, just the “starting point” of the lookup process is different.

Thanks for the reply!
They are in two different ViewGroups, so there are no parent-child relationships between them.

as I see in the link I sent you, following-siblings selects the siblings after the current node.
not sure why it shows you the same id but try to write something like:

try to add /parent::*/ or /parent::android.view.ViewGroup/ the moment before using following-siblings.

Suggested selector
//*[contains(@text,'Subtotal')]/parent::*/following-sibling::android.widget.TextView[@resource-id=''] will not work because /parent::*/ part will return android.view.ViewGroup and following-sibling to it is another android.view.ViewGroup, not desired android.widget.TextView

So //*[contains(@text,‘Subtotal’)] is the current node and android.widget.TextView[@resource-id=‘’] is the sibling, following to the current node

Moreover, I discovered that following sibling is not even a problem in my case, since the appium returns the same element (again, rather rarely) for simple selectors like //*[contains(@text,'Subtotal')] and //*[contains(@text,'Order Tortal')]

I can see you have an additional Order Total text in your pic.
try to “complex” the xpath and don’t use * sign, make it more accurate.