How to automate mouse scrolling in selenium | Selenium Forum
M
Posted on 04/08/2016
Go to below url

http://demoqa.com/accordion/

click on "Section 2" and scroll it.

How can I scroll over mouse in Selenium.

M
Replied on 04/08/2016

package book;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
// Covered in Module 16 Google_Page_Navigation.java


public class scroll {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

driver.get("http://demoqa.com/accordion/");

driver.manage().window().maximize();


driver.findElement(By.xpath(".//*[@id='ui-accordion-accordion-header-1']")).click();
WebElement element = driver.findElement(By.xpath(".//*[@id='ui-accordion-accordion-panel-1']"));

Long height = (Long) ((JavascriptExecutor) driver).executeScript("return arguments[0].scrollHeight;", element);

System.out.println("The height is " + height.toString());
Long newScrollHeight = 0L;

while(height > newScrollHeight)

{

((JavascriptExecutor) driver).executeScript("return arguments[0].scrollTop = " + newScrollHeight.toString() + ";", element);

newScrollHeight = newScrollHeight + 10;

Thread.sleep(3000);
}

}

}