Can't get setMaxSearchSwipes() to work with driver.findElement(MobileBy.AndroidUIAutomator)

I am trying to find certain cards or tiles in an Android Recycler view, using the below code I can get it to work sometimes.

    MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceId(\"" + resourceID + "\")).getChildByText("
                    + "new UiSelector().className(\"" + className + "\"),\"" + buttonText +"\")"));

The issue is, about half the time it will quit looking before finding the desired text, so I’ve been trying to use the above code with setMaxSearchSwipes(20)

I’ve tried the following, but the code seems to only try swiping 5 times before it quits.

    MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceId(\"" + resourceID + "\")).setMaxSearchSwipes(20).getChildByText("
                    + "new UiSelector().className(\"" + className + "\"),\"" + buttonText +"\")"));

and

    MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
            "new UiScrollable(new UiSelector().resourceId(\"" + resourceID + "\")).getChildByText("
                    + "new UiSelector().className(\"" + className + "\"),\"" + buttonText +"\").setMaxSearchSwipes(20)"));

why you do not use " scrollIntoView" ?

Hey Aleksei,

Thank you for the response! I’ve tried a bit of everything I’ve found on different support articles, I had tried the scrollIntoView previously and couldn’t get it to work, but I think I was doing it wrong.

I tried the following with scrollIntoView and it is working much more consistently:
MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
“new UiScrollable(new UiSelector().resourceIdMatches(”" + resourceID + “”)).setMaxSearchSwipes(15).scrollIntoView("
+ “new UiSelector().text(”" + buttonText + “”))"));

Thanks a bunch!

try -> Find element with certain text inside another element using UISelector query

magic in “.scrollable(true)” plus you can add “setMaxSearchSwipes” PLUS add direction if it e.g. horizontal.

1 Like