How to get all the elements of same resource id in uiautomator?

3.3k views Asked by At

I am new to uiautomator.In a screen i want to select an element with its resource id.But there is so many elements have the same resource id(for example in Instagram app profile all videos and images having the same resource id ).Is there any way so that i can get a list of all these elements with same resource id ?Only distinguishable feature for the element is its index.

2

There are 2 answers

0
Inês On

You can get all the elements with a UiCollection instead of a UiObject:

UiCollection element = new UiCollection(new UiSelector()....)

However I'm not sure how to handle this afterwords. I'm facing a similar problem myself.

0
mikus420 On
public ArrayList<UiObject> getComponents(UiSelector selector) {
    ArrayList<UiObject> objects = new ArrayList<UiObject>();
    boolean next = true;
    int i = 0;
    while(next) {
        UiObject obj = new UiObject(selector.instance(i++));
        if(obj.exists())
            objects.add(obj);
        else
            next = false;
    }
    return objects;
}

It will get all components in ArrayList of UiObject classes, which met selector condition.

Just keep author and license pls:)

Copyright (c) 2016 Dariusz Mika

http://www.apache.org/licenses/LICENSE-2.0