Appuim2 not able to identify android elements after scrolling down the mobile screen

Description: The issue described below exists only for Android and not IOS
When I scroll down the mobile device, then the appium stops identifying the elements on the screen after scrolling. As a workaround, I need to switch to web view, perform the action on the element and then switch back to web view. This started happening after I moved to appium 2.

Capabilities defined for device: “appiumVersion”: “2.0.0”, “automationName”: “uiautomator2”

appium java client version in pom.xml: 9.2.0
selenium java client version in pom.xml: 4.18.1

What method/approach do you use for Scrolling? W3C, TouchAction or UiScrollable ?

Hi , I am using TouchAction to scroll. Tried UiScrollable as well but UiScrollable does not work for Android as Android elements are not available until they are in the view.

@parulbhagat-SBS UI scrollable actually works for elements not in view, if you add arg “scrollIntoView”. Basically it will try to scroll multiple times looking for the element that matches the ID/Text/Content-desc.
UiScrollable is the most stable too.

Please try:
given element you want has resource-id: “test1” and index is 0
call the method: scrollToID(“test1”,0);
Method below:

    public void scrollToID(String id, int index) throws InterruptedException {
        try {
            androidDriver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\"" + id + "\").instance(" + index + "))"));
        }catch (InvalidSelectorException ignored){}
        Thread.sleep(1000);
    }

Lastly TouchAction is now deprecated. Please try W3C

@derolk Some of our elements are identified by xpath instead of ID/Text/Content-desc due to development restrictions in the application . How can I scroll in such case as for xpath I could not find any function like you mentioned for resourceId .