Search code examples
iosautomationappiumdom-eventsjava-client

How to scroll to element which is not visible in iOS using appium


I was using the following code to scroll to an element which is not visible:

WebElement element = driver.findElementByName("text");

        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, String> scrollObjects = new HashMap<String, String>();
        scrollObjects.put("element", ((RemoteWebElement) element).getId());
        js.executeScript("mobile: scrollTo", scrollObjects);

I am getting below error when I use above method for scrolling:-

Unknown command, all the mobile commands except scroll have been removed.

In Appium 1.5.0 , name locator is removed

In java-client v4.0.0, scrollTo() and scrollToExact() became deprecated.

I am using xpath instead of findElementByName().

What is the workaround for mobile: scrollTo


Solution

  • I have found answer for this issue. Use element and direction as arguments to scroll method.

    WebElement element = driver.findElementByName("text");
    
    JavascriptExecutor js = (JavascriptExecutor) driver;
    
    HashMap scrollObjects = new HashMap();
    scrollObjects.put("element", ((RemoteWebElement) element).getId());
    scrollObjects.put("direction", "down");
    driver.executeScript("mobile: scroll", scrollObjects );
    

    References:- https://pioneer2k9.blogspot.in/2016/08/mobile-scroll-command-is-not-working-in_4.html