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
driver.scrollTo(“SomeElementText”);
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
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);
}
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);
}