Categories
java javascript selenium webdriver windows

Scrolling an ajax page completely using selenium webdriver

I am trying to scroll a page completely using this code:

JavascriptExecutor js = (JavascriptExecutor) Browser;
js.executeScript("javascript:window.onload=toBottom();"+
"function toBottom(){" +"window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +"document.body.scrollHeight,document.documentElement.clientHeight));" +"}");
js.executeScript("window.status="fail";");
//Attach the Ajax call back method
js.executeScript( "$(document).ajaxComplete(function() {" + "status="success";});");
js.executeScript("window.status="fail";");
//Attach the Ajax call back method
js.executeScript( "$(document).ajaxComplete(function() {" +"status="success";});");

This code works fine and scroll the page for the first attempt but when page is scrolled down, new data appears at the page and this code failed to scroll it again.
So what I need is that someone will help me to scroll the page till end until scrolling is completed.

Do I use any loop for this?

Help/Suggestions/Response will be appreciated!