scrollIntoView not working in appium for log scroll view. Throws no such element exception after one/two swipes

1.2k views Asked by At

Generic function for Scroll where scrollablelist is the locator of scroll view in which we have to scroll

public void scrollToElementWithText(RemoteWebDriver driver, String scrollableList, String text) {

        MobileElement element = (MobileElement) driver
                .findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().resourceId(\""
                        + scrollableList + "\")).scrollIntoView(" + "new UiSelector().text(\"" + text + "\"))"));
    }
1

There are 1 answers

2
Muzzamil On

You can try below code with in existing code scrollable(true).instance(0))

Add in desired capability UiAutomator2 if you are using appium as automation engine.

capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");

Now use below functions if you have element's id, and index as 0 for there is one element on page.

public void scrollByID(String Id, int index) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().resourceId(\""+Id+"\").instance("+index+"));")); 

        } catch (Exception e) {
           e.printStackTrace();
        }
    }

Scroll using Text

public void scrollByText(String menuText) {

        try {

             driver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textMatches(\"" + menuText + "\").instance(0));")); 
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

Scroll using help of screen size:

public void scrollToBottom() {

      int  x = driver.manage().window().getSize().width / 2;
      int start_y = (int) (driver.manage().window().getSize().height * 0.2);
      int end_y = (int) (driver.manage().window().getSize().height * 0.8);
        TouchAction dragNDrop = new TouchAction(driver)
                        .press(PointOption.point(x,start_y)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(500)))
                        .moveTo(PointOption.point(x, end_y))
                        .release();
        dragNDrop.perform();
    }