How to ScrollToElement in appium v1.0.0-beta.5

Hi guys,

I’ve searched about the scrolling and swiping movements but doesn’t find any solution for this.

This method I’m using for the scroll.

public void scrollToElement(WebElement el) {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<>();
scrollObject.put(“element”, ((RemoteWebElement) el).getId());
scrollObject.put(“direction”, “down”);
js.executeScript(“mobile: scroll”, scrollObject);
}

public void tapOnScrolledElement(){
List listOfElements = utils().iOSDriver().findElements(org.openqa.selenium.By.id(“idOfList”));
int index = 7;
scrollToElement($(listOfElements().get(index));
}

doc - https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/ios-xctest-mobile-gestures.md

actually:

[XCUITest] Error: Mobile scroll supports the following strategies: name, direction, predicateString, and toVisible. Specify one of these

as you see “element” is missing and it is true - https://github.com/facebook/WebDriverAgent/blob/master/WebDriverAgentLib/Commands/FBElementCommands.m

BUT thanks for question! i updated mine code more easy.

mine previous working approach ( i just scroll slightly until element becoming visible):

    public Boolean tapRequestByDescription(String descr) {
        System.out.println("  tap request by description: " + descr);
        MobileElement el = (MobileElement) ((IOSDriver) driver).findElementByIosNsPredicate("value = '" + descr + "'");
        int y;

        if (el != null ) {
            y = el.getCenter().getY();
            while (y <= 0) {
                scrollToDirection_iOS(mainContainer.get(0), "u", 120);
                y = el.getCenter().getY();
            }
            try {Thread.sleep(700);} catch (Exception e) {}
            return tapElement(el);
        }

        return false;
    }

now i changed to:

    public boolean tapPaymentByDescription(String text) {
        System.out.println("   tapPaymentByDescription(): " + text);

        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap scrollObject = new HashMap<>();
        scrollObject.put("predicateString", "value == '" + text + "'");
        js.executeScript("mobile: scroll", scrollObject);

        WebElement el = ((IOSDriver) driver).findElementByIosNsPredicate("value = '" + text + "'");
        return  tapElement((MobileElement) el);
    }

Hi @Aleksei,

 public WebElement scrollToElementNew(String text) {
    System.out.println("   tapPaymentByDescription(): " + text);
    JavascriptExecutor js = (JavascriptExecutor) driver;
    HashMap scrollObject = new HashMap<>();
    scrollObject.put("predicateString", "value == '" + text + "'");
    js.executeScript("mobile: scroll", scrollObject);
    return iOSDriver().findElementByIosNsPredicate("value = '" + text + "'");
}

public void tapOnScrolledElement(WebElement element) {
    element.click();
}

The element is found, the text is printed, BUT :slight_smile: the error comes with the following:
[main] ERROR net.serenitybdd.core.Serenity - An unknown server-side error occurred while processing the command. Original error: Can’t scroll to element that does not exist (WARNING: The server did not provide any stacktrace information)

I’m calling the scroolToElement method like this:

String name = channelsList().get(index).getText();
    utils().tapOnScrolledElement(utils().scrollToElementNew(name));

maybe scrollView does not exists in your app. then switch to mine first approach - do swipe yourself and check element to tap location.

what makes this? Can you give please more details? Thanks

It is just custom function. Inside driver.swipe(). Just sometimes i need to swipe not all page but only partial.

Is scrollObject.put(“predicateString”, “value == '” + text + “'”); working for you ? @sivanov for me its just scrolling up & down, is it moving to a particular element ?

@dilsreccse you can use whatever attribute is needed “value” “name” … just check in driver.getPageSource() your text

Hi @Aleksei,
I’ve solved this like:

public void scrollAndTapAtChannelWithText(String elementName) {
    WebElement el =
            utils.iOSDriver().findElementByAccessibilityId("channel" + elementName + "Cell");
    JavascriptExecutor js = (JavascriptExecutor) getDriver();
    HashMap<String, String> channelObject = new HashMap<>();
    channelObject.put("element", ((IOSElement) el).getId());
    channelObject.put("toVisible", "true");
    channelObject.put("direction", "down");
    channelObject.put("x", String.valueOf(el.getSize().getWidth() / 2));
    channelObject.put("y", String.valueOf(el.getSize().getHeight() / 2));
    js.executeScript("mobile: scroll", channelObject);
    js.executeScript("mobile: scroll", channelObject);
    js.executeScript("mobile:tap", channelObject);
}

@sivanov interesting :slight_smile:

public void scrollToElement(int index, List<IOSElement> listOfElements) {
    IOSElement el = listOfElements.get(index);
        JavascriptExecutor js = (JavascriptExecutor) getDriver();
        HashMap<String, String> scrollObject = new HashMap<>();
        scrollObject.put("element", el.getId());
        scrollObject.put("toVisible", "true");
        js.executeScript("mobile: scroll", scrollObject);
}

or like this :slight_smile:

public String scrollToElementNew(String text) {
    IOSElement element =
            iOSDriver().findElementByIosNsPredicate("value = '" + text + "'");
    try {
        JavascriptExecutor js = (JavascriptExecutor) getDriver();
        HashMap<String, String> scrollObject = new HashMap<>();
        scrollObject.put("toVisible", "true");
        scrollObject.put("predicateString", "value == '" + text + "'");
        js.executeScript("mobile:scroll", scrollObject);
        element.click();
        return element.getText();
    } catch (Exception e) {
        return element.getText();
    }
}

Hi…
I have used this code for iOS scroll to text. but for me it scroll to the end of the list and keeps on scrolling and not finding the element.
[HTTP] {“script”:“mobile: scroll”,“args”:[{“predicateString”:“value == ‘Turkey’”}]}
[MJSONWP] Calling AppiumDriver.execute() with args: [“mobile: scroll”,[{“predicateString”:“value == ‘Turkey’”}],“77c45bb1-9ee5-4260-851c-284bdc85aace”]
[XCUITest] Executing command ‘execute’… “IT keeps scrolling end of the list”. Please help.

Did you get the solution for scrolling by text… mine is scrolling down only, not sure that it is location the text…