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 -> -> 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 + "\"))"));

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(“”)).setAsHorizontalList().scrollIntoView(”
+ “new UiSelector().descriptionContains(“Family”))”));

Hi @Aleksei
Is this solution still valid?

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 + "\"))"));;

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

1 Like

You miss here one “)” before dot.

More examples Swipe Tutorial - Appium

1 Like

I am trying to use scroll horizontally using the below code and it works intermittently. Can someone please suggest me a solution.

driver.findElement(new AppiumBy.ByAndroidUIAutomator(“new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector()”+ “.textStartsWith("”+“VOD”+ “").instance(0));”));

I tried below as well but of no use.

driver.findElement(new AppiumBy.ByAndroidUIAutomator(“new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains("VOD Carousel"));”));

I used textContains and text as well but it doesnot work all the time.