Selenium: Scroll to end of page in dynamically loading webpage
I will provide you code in Python for this. I think it's easy to translate to Java:
def scroll_down(self):
"""A method for scrolling the page."""
# Get scroll height.
last_height = self.driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to the bottom.
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load the page.
time.sleep(2)
# Calculate new scroll height and compare with last scroll height.
new_height = self.driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
Hope it helps you!
Updated Johannes code a bit to make it functional.
JavascriptExecutor js = (JavascriptExecutor) driver;
try {
long lastHeight=((Number)js.executeScript("return document.body.scrollHeight")).longValue();
while (true) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
Thread.sleep(2000);
long newHeight = ((Number)js.executeScript("return document.body.scrollHeight")).longValue();
if (newHeight == lastHeight) {
break;
}
lastHeight = newHeight;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Making a slight correction to the above stated answers. The variable 'start' of type long keeps changing after every scroll and the value becomes same after it reaches the end of the webpage. And as it an infinite loop with will keep returning the same value again and again. So, I just took the 'temp' variable and checked two consecutive values are same or not as the values remain same after the end is reached. As soon at it finds the same it exits the loop.
try {
long temp = 0;
while (true) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
Thread.sleep(2500);
long start = (Long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
if (start == temp) {
break;
}
temp = start;
}
System.out.println("completed the scroll");
} catch (Exception e) {
e.printStackTrace();
}
Thanks to Ratmir Asanov (see the approved answer above), I translated the Python code into Java to make it easier to implement for other people.
try {
long lastHeight = (long) ((JavascriptExecutor) webDriver).executeScript("return document.body.scrollHeight");
while (true) {
((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
Thread.sleep(2000);
long newHeight = (long) ((JavascriptExecutor) webDriver).executeScript("return document.body.scrollHeight");
if (newHeight == lastHeight) {
break;
}
lastHeight = newHeight;
}
} catch (InterruptedException e) {
e.printStackTrace();
}