How to scroll on Android without using coordinates

Is there a way to scroll on Android devices (up, down, left, right) without using coordinates or elements but directly on the screen?

The following code snnipet provides error:
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put(“direction”, “down”);
js.executeScript(“mobile: scroll”, scrollObject);

Error:

org.openqa.selenium.WebDriverException: ERROR running Appium command: cb is not a function

If you use java client version 3.2 or 3.3 then lot of good methods are there for scroll

These are bit unreliable methods for scrolling

  1. driver.scrollTo(“SomeElementText”);
  2. driver.scrollToExact(“SomeElementText”);

// 3. Using Swipe Method
int startX = driver.findElement(By.name(“scroll from element name”)).getLocation().getX();
int startY = driver.findElement(By.name(“scroll from element name”)).getLocation().getY();
int endX = driver.findElement(By.name(“scroll TO element name”)).getLocation().getX();
int endY = driver.findElement(By.name(“scroll TO element name”)).getLocation().getY();

3.1 driver.swipe(startX,starY,endX,endY,2000); // swipe using element coordinates

In this swipe method you can also pass webelement like

WebElement fromElement= driver.findElement(By.name(“scroll from element name”));
WebElement toElement= driver.findElement(By.name(“scroll TO element name”));

3.2.1 driver.swipe(fromElement,toElement,2000);
3.2.2 driver.swipe(toElement,fromElement,2000); // swipe in reverse direction if we pass element in reverse order

// 4. Scroll using TouchActions

WebElement fromElement= driver.findElement(By.name(“scroll from element name”));
WebElement toElement= driver.findElement(By.name(“scroll TO element name”));
TouchAction tAction=new TouchAction(_driver);
tAction.press(fromElement)
.moveTo(toElement)
.release()
.perform();

Note : Here we can create WebElement based on any locator strategy

Is there a solution without the use of Elements also … only to scroll on the screen as I do manually and release after some time?

There isn’t such a built in function.
You need to provide either an elements text to scroll to this element or use coordinates.
With that you can build your own scrollDown method like this:

static void scrollDown() {
Dimension dimens = driver.manage().window().getSize();
int x = (int) (dimens.getWidth() * 0.5);
int startY = (int) (dimens.getHeight() * 0.5);
int endY = (int) (dimens.getHeight() * 0.2);
driver.swipe(x, startY, x, endY, 800);
}

1 Like

static void scrollDown() {
Dimension dimens = driver.manage().window().getSize();
int x = (int) (dimens.getWidth() * 0.5);
int startY = (int) (dimens.getHeight() * 0.5);
int endY = (int) (dimens.getHeight() * 0.2);
// lest say if above code calculated correct scroll area as per your device then just add static loop to scroll to no of times you want to scroll
for (int i=0;i<10;i++)
driver.swipe(x, startY, x, endY, 800);
}

Thanks a lot amitjaincoer191 … Used and it is very helpful!