How to use the UiScrollable function in Appium when the scrollIntoView text is passed through a variable?

If the country i.e. Algeria is directly passed in place of country in quotes, it works fine, however, if the variable is used, an error pops up.

Please suggest how to scroll and find an element, when the element is in a variable.

String country = “Algeria”;
driver.findElementByAndroidUIAutomator(“new UiScrollable(new UiSelector()).scrollIntoView(text(country))”).click();

Error: Could not parse expression new UiScrollable(new UiSelector()).scrollIntoView(text(country)) : UiScrollable doesn’t have suitable method scrollIntoView with arguments [text(country)]

try -> https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/tutorial/swipe-tutorial.md -> Android: ‘UIScrollable’ swipe

1 Like

Thanks a lot, but all of these methods require one to pass values directly as String.
There’s nothing mentioned where we replace it with its variable and it’ll work the same.

it simple as:

        String country = "Algeria";
        MobileElement element = (MobileElement) driver.findElement(MobileBy.AndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true))" +
                        ".scrollIntoView(new UiSelector().text(\"" + country + "\"))"));
2 Likes

Thanks so so much for the help! It works! I didn’t understand the significance of the extra quotes first. Now got it.

for horizontal resource id i am not able to get it in appium inspector , is there any other option to replace resource id
MobileElement element = driver.findElement(MobileBy.AndroidUIAutomator(
“new UiScrollable(new UiSelector().resourceId(“com.android.vending:id/items”)).setAsHorizontalList().scrollIntoView(”
+ “new UiSelector().descriptionContains(“Family”))”));

Hi @Aleksei
Is this solution still valid?

NOTE:
MobileBy was deprecated and replaced by AppiumBy

Just replace with AppiumBy and try…

driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"Your Text\").instance(0))")).click();

Where driver is AndroidDriver

What does “new” stands here for?

I entered the code below

String country = "Nigeria";
       WebElement selectCountryCode = driver.findElement(new AppiumBy.ByAndroidUIAutomator(
                "new UiScrollable(new UiSelector().scrollable(true)" +
                        ".scrollIntoView(new UiSelector().text(\"" + country + "\"))"));
        selectCountryCode.click();

But got this error:
org.openqa.selenium.InvalidSelectorException: Could not parse selector expression new UiScrollable(new UiSelector().scrollable(true).scrollIntoView(new UiSelector().text("Nigeria")): Unclosed paren in expression

Any guide on how to resolve this will be much appreciated. I couldn’t figure out where the unclosed paren issue is coming from

MobileElement is deprecated so I had to opt for WebElement

You miss here one “)” before dot.

More examples Swipe Tutorial - Appium