I am currently automating tests for a native app and I have some issues with "ghosts" elements. By "ghosts elements" I mean elements that are in the code and technically on the page but aren't visible to the user.
I am testing using Android and I don't know if the issue would also be present with iOS.
Simply put, I want to click on element X but instead I end up clicking on element Y who isn't visible to the user because X and Y share the same specifications (same text, same element type/class).
I have tried ignoring the element Y by using "isDisplayed()" and "isEnabled()" but it seems like both X and Y are displayed and enabled (even though only X is actually visible to the user).
Is there any way I can discriminate between X and Y using the fact that one is visible to the user and the other isn't? If possible, I don't want to ask the devs to change the code of the app.
If it helps, here is how the app ended up with "ghost elements":
Apps open page 1, page 1 has element Y
Apps open page 2, page 2 is different that page 1 but have element X who is the same as element Y from page 1
We are now on page 2 but page 1 didn't disappear and is just "behind" page 2.
PS: I am using Java but I would accept answers in any language.