[WebdriverIO] Trouble scrolling to element on Android

I have WDIO, Appium, JS for Android automation.
appium-uiautomator2-driver version is 3.8.0
appium version is 2.11.3

Trying to execute ‘mobile: scroll’ according to documentation, but it’s not working

await driver.execute('mobile: scroll', {
  strategy: '-android uiautomator', 
  selector: 'new UiSelector().resourceId("home_feed_start_searching")', 
  maxSwipes: 10
});

Appium logs saying ‘no such element’ even though Appium Inspector recognize it.

 script: 'mobile: scroll',
[0-0]   args: [
[0-0]     {
[0-0]       strategy: '-android uiautomator',
[0-0]       selector: 'new UiSelector().resourceId("Popular Properties")',
[0-0]       maxSwipes: 10
[0-0]     }
[0-0]   ]
[0-0] }
[0-0] 2024-10-18T14:53:39.351Z INFO webdriver: RESULT {
[0-0]   error: 'no such element',
[0-0]   message: 'androidx.test.uiautomator.UiObjectNotFoundException: Cannot scroll to UiSelector[RESOURCE_ID=Popular Properties]',

I want to have a function similar to ’ mobile: scrollToElement’ in iOS but on Android.

Log doesn’t seem to correspond to your code. Looks like 2 very different elements. Maybe that’s the problem?

Best suggestion seems to be to use scrollIntoView for this:

@wreed You’re right, my mistake. But it won’t change the result - it’s not scrolling correctly.
Here is an error I get:

INFO webdriver: RESULT {
[0-0]   error: 'no such element',
[0-0]   message: 'androidx.test.uiautomator.UiObjectNotFoundException: Cannot scroll to UiSelector[RESOURCE_ID=home_feed_start_searching]',
[0-0]   stacktrace: 'io.appium.uiautomator2.common.exceptions.ElementNotFoundException: androidx.test.uiautomator.UiObjectNotFoundException: Cannot scroll to UiSelector[RESOURCE_ID=home_feed_start_searching]\n'

It scrolls to the top of the page, then just ONE scroll down and stops, but the element is lower on the page.

Also tried

await (await homeFeedPage.startSearchingBtn).scrollIntoView();

where

get startSearchingBtn() { return $('//*[@resource-id="home_feed_start_searching"]'); }

Error:

INFO webdriver: COMMAND findElement("xpath", "//*[@resource-id="home_feed_start_searching"]")
[0-0] 2024-10-18T18:51:18.912Z INFO webdriver: [POST] http://localhost:4723/session/a7ae6152-5523-4a58-8a41-bf821a62f756/element
[0-0] 2024-10-18T18:51:18.913Z INFO webdriver: DATA {
[0-0]   using: 'xpath',
[0-0]   value: '//*[@resource-id="home_feed_start_searching"]'
[0-0] }
[0-0] 2024-10-18T18:51:18.986Z INFO webdriver: RESULT {
[0-0]   error: 'no such element',
[0-0]   message: 'An element could not be located on the page using the given search parameters.',
[0-0]   stacktrace: 'NoSuchElementError: An element could not be located on the page using the given search parameters.\n'

Probably because element is out the viewport.

I am also in the native Android app.

@Aleksei, please advise if possible, TIA.

Try something like pure UIAutomator command

// Java
// first scroll to begining
try {
  driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)).scrollToBeginning(10)"));
} catch (Exception ignored) {}
// now try scroll to needed element ->
WebElement el = driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)).setAsVerticalList().scrollIntoView(new UiSelector().resourceId(\"your_ID\"))"));