How to Scroll element to particular position in appium 1.7 for iOS

Viewed 7896

Below code is working for scroll to end but I don't want scroll till end I want to scroll some particular position:

driver.findElementByXPath("//*[@value='UpcomingInstallations']").click();
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("direction", "down");
js.executeScript("mobile: scroll", scrollObject);
6 Answers

My method of doing this is to look for the element on the screen (https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#isDisplayed--), and if I don't see it, swipe.

while (!displayed)
   swipe

I'd recommend taking a look at the TouchAction class for your swiping: https://appium.github.io/java-client/io/appium/java_client/TouchAction.html. The preferred method of swiping is the following:

myTouchAction.press(startX,startY).moveTo(endX,endY).release().perform()

Since pixels can vary between devices, you're going to want to use coordinates that are based on percentages of the total screen size. See https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.Window.html#getSize-- for more information.

This is working for me (Python code):

self.touchAction.press(None, x, self.screenHeight/4).move_to(None, 0, (self.screenHeight/4)*-1).release().perform()

Let me now is works for you

driver.findElementByXPath("//*[@value='UpcomingInstallations']").click();

JavascriptExecutor js = (JavascriptExecutor) driver;

HashMap scrollObject = new HashMap();

scrollObject.put("direction", "down");

js.executeScript("mobile: scroll", scrollObject);

This definitely works.

In my case (using Java), I performed this for a simple UIListView and it works (in Appium-mac-1.15.0-1) for that end since driver.swipe method is deprecated:

There you go

public static void scrollDownIos(IOSDriver<IOSElement> driver, double scrollPercentageStart, double scrollPercentageEnd) 
{
    Dimension size = driver.manage().window().getSize();
    int x = size.getWidth()/2;
    int starty = (int) (size.getHeight() * scrollPercentageStart);
    int endy = (int) (size.getHeight() * scrollPercentageEnd);
    (new TouchAction(driver)).press(PointOption.point(x, starty))
    .waitAction(WaitOptions.waitOptions(Duration.ofMillis(2000)))
    .moveTo(PointOption.point(x, endy))
    .release()
    .perform();
}

In your scroll object just give a locator value. Your locator can be any of the available e.g : ID, name, xpath or few other are available.

JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, String> scrollObject = new HashMap<String, String>();
        scrollObject.put("direction", "down");
        scrollObject.put("xpath", "<your xpath value>");
        js.executeScript("mobile: scroll", scrollObject);
Related