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,'Subtotal')]/following-sibling::clazzName[@resource-id='resourceId']
and
//*[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?
see: https://www.w3schools.com/xml/xpath_axes.asp#:~:text=the%20current%20node-,following-sibling,-Selects%20all%20siblings

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='com.arammeem.toyou.android:id/valueTextView'] 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=‘com.arammeem.toyou.android:id/valueTextView’] 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.