seleniumappiumselenide

How can I find element by non-unique resousre-id?


I test an application which use non-unique resourse-id for elements.

Is there any way to find such elements by xpath like

//*[@resourse-id='non-unique-id'][2]

I mean the second element with same resourse-id.


Solution

  • I'd recommend avoiding xpath in mobile automation since this is the most time-consuming strategy to find elements. If you don't have any other anchors for your elements but you confident in its order, you can stick to the following approach: Appium driver can return a list of elements with the same locator, in case of Page Object model you can either do this way:

    @AndroidFindBy(uiAutomator = "resourceIdMatches(\".*whatever\")")
    private List<MobileElement> elements;
    

    so, once your page is initialized, you can access an element by index:

    elements.get(1).click();
    

    or, in case of manual managenemt, you can do this way:

    List<MobileElement> elements = driver.findElements(MobileBy.AndroidUIAutomator("resoureceIdMatches(\".*whatever\")"));
    elements.get(3).click();
    

    Hope this helps.