Perform Scroll to Element 

Many times WebElements are not recognised by WebDriver even if element is present in the DOM. WebDriver fails to find the element which is not visible in browser’s visible area. For this we have to make the WebElement view-able by WebDriver by scrolling to the element. We can scroll into view in Selenium using JavaScript executor. JavaScriptExecutor interface helps to execute JavaScript methods through Selenium Webdriver. 

Syntax: JavascriptExecutor js = (JavascriptExecutor) driver
              js.executeScript(Script,Arguments);

//Create an instance of Javascript Executer
JavascriptExecutor js = (JavascriptExecutor) driver;


//Scroll down the page by 500 pixel vertical- Example1
js.executeScript("window.scrollBy(0,500)");


//Scroll to the web element in the page- Example2
js.executeScript("arguments[0].scrollIntoView();", WebElement);


//Scroll to the bottom of the page- Example3
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");