How to find Android elements that have the same Resource-Id but different Indexs and Names

I have lots of Android elements that have the SAME Resource-Id but different Indexes.

The first element with Index = 1 is shown here:
image

They also have different Text values as shown.

Anyone know the best way of finding these elements?

I was thinkingof using @AndroidFindBy(uiAutomator = "new UiSelector()…

to search for the class and then the Index but I’m not sure how to do it.

Any help would be appreciated.

Thanks!

Im using

@AndroidFindBy(uiAutomator = "new UiSelector().text(\"my text\")")

when I need to find the elements by text and I’m sure that no other element with the same text exist in the page.

also you could try something like

@AndroidFindBy(id = "yourID")
public List<MobileElement> element;

and then use
element.get(elementIndex).click();

I’m not using the index shown in the inspector. First element in the page from top to bottom has index 0, second element has index 1 and so on.
Sometimes I encounter errors when I used the index from the inspector

Thanks Zuzeac,

Regarding this:
@AndroidFindBy(uiAutomator = “new UiSelector().text(“my text”)”)

There are 2 elements that have the same text: a title element and the value element. Is there a way to select the 2nd element that has that text?

Regading your 2nd method:

@AndroidFindBy(id = “yourID”)
public List element;
element.get(elementIndex).click();

I was using that method…but some of the elements are off screen so when you create your list it can’t see them and doesn’t add them to the list. So maybe only the top 4 elements are included in the list but the other 6 that are off screen are not included.

Add uiscrollable and appium will scroll to element outside screen. Check examples here in search.

Great thanks guys…I have got this code working to find 1 element:

    WebElement firstName = driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceId(\"com.gbg.verify.gbgtestapp:id/people_form_recyclerview\")).getChildByText("
                    + "new UiSelector().resourceId(\"com.gbg.verify.gbgtestapp:id/formElementValue\"), \"First Name\")"));

But I have about 10 elements do I have to write all that code for each element? I could write a method I suppose but is there an easier way.

Thanks again :slight_smile:

@Matt882 some updates:

  1. resourceId -> resourceIdMatches
new UiSelector().resourceIdMatches(\".*:id/people_form_recyclerview\")
  1. instead of “First Name” use variable
function WebElement getElementByText(String text)
return driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceIdMatches(\".*:id/people_form_recyclerview\")).getChildByText("
                    + "new UiSelector().resourceIdMatches(\".*:id/formElementValue\"), \"" + text + "\")"));

Thank you. Its working fine.
How to find the number of elements(size) in the page with same resource id.

You have one regex error with which it didn’t work for me.

resourceIdMatches(\".*:id/people_form_recyclerview\")

Pay attention to the dot in the regex (.*). Otherwise it won’t work.

yes. thanks. updated. just misprinted.