29

I have a webpage that keeps loading new items when scrolling down the page until every item is loaded.

I'm working with Selenium in Java, and need to scroll down to the bottom of the page in order to load everything.

I have tried several different options, like scrolling to an element of the bottom of the page:

WebElement copyrightAtEndOfPage = webDriver.findElement(By.xpath("//a[@href='/utils/copyright.html']"));
((JavascriptExecutor) webDriver).executeScript("arguments[0].scrollIntoView();", copyrightAtEndOfPage);

This only scrolls down once though, and then the webpage keeps loading.

I also tried this approach, which also only scrolls down once, because it only takes the browser height into consideration.

Any help is highly appreciated.

1
  • Can we also do similar thing using JS? Commented Aug 5, 2020 at 13:42

11 Answers 11

63

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!

Sign up to request clarification or add additional context in comments.

7 Comments

That works just perfectly, thank you so much! I will also post an answer with the code that I translated into Java.
Did it. Thanks again!
hey.. thanks for the answer.. this should help me out as well! Just a query, when I use this code, my browser does scroll to the end but the new elements are not being captured still. I am a bit new to this. I am using driver.get(url) followed by the above code.. could you help?
@ShrutiJoshi, this code just will scroll down to the bottom of the visible page (on the screen), when you call this function.
@my previous comment. It looks like this website is using a different selector. so instead of using "driver.execute_script("return document.body.scrollHeight")" it uses "driver.execute_script("return document.documentElement.scrollHeight")". This has solved my problem
|
13

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();
}

Comments

3

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();
}

2 Comments

typecasting was the issue, It was throwing compilation error.
What was the error? Because I never had any issues with that code and I used it 1:1 in my project like that.
3

C# version of the Ratmir Asanov's answer:

var lastHeight =  driver.ExecuteScript("returndocument.body.scrollHeight");
while (true)
{
    driver.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);");
    await Task.Delay(500);

    var newHeight = driver.ExecuteScript("return document.body.scrollHeight");
    Console.WriteLine(lastHeight + " - " + newHeight);
    if (newHeight.Equals(lastHeight))
        break;

    lastHeight = newHeight;
}

Comments

1

I found another solution to the dynamically loading page.

Count the elements that are displayed every scroll before and after the scroll and compare them to determine if you've scrolled to the bottom.

var reachedEnd = false;
oldCount = driver.FindElements(By.CssSelector(".searchDataContainer.table-row.raw")).Count;

while (!reachedEnd)
{
    driver.FindElement(By.CssSelector("body")).SendKeys(Keys.End);
    Thread.Sleep(500);
    oldCount = driver.FindElements(By.CssSelector(".searchDataContainer.table-row.raw")).Count;

    if (newCount == oldCount)
    {
        reachedEnd = true;
    }
    else
    {
        newCount = oldCount;
    }
}

Comments

1

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();
        }

Comments

0

Updating the above solution by Prabhat further as it was still giving me compilation error.

    try {
        Object lastHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");

        while (true) {
            ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
            Thread.sleep(2000);

            Object newHeight = ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
            if (newHeight.equals(lastHeight)) {
                break;
            }
            lastHeight = newHeight;
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

Comments

0

Updated code that worked for me:

try {
                    long lastHeight = (long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
                    int cont=1000;
                    while (true) {
                        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, "+cont+");");
                        Thread.sleep(2000);

                        long newHeight = (long) ((JavascriptExecutor) driver).executeScript("return document.body.scrollHeight");
                        if (newHeight <= cont) {
                            break;
                        }
//                      lastHeight = newHeight;
                        cont+=500;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You can add the following code to keep pressing page Down button:

new Actions(driver).sendKeys(Keys.PAGE_DOWN).perform();

Comments

0

to scroll and wait to load more property till end

                lenOfPage = driver.instance.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")

                match = False
                while not match:
                    lastCount = lenOfPage
                    time.sleep(2)
                    lenOfPage = driver.instance.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")

                    if lastCount == lenOfPage:
                        match = True

Comments

0
modal_elem = browser.find_element(By.CLASS_NAME,'yourClassName')`
def scroll_down(self):
"""A method for scrolling the page."""

    # Get scroll height.
    last_height = self.execute_script("return arguments[0].scrollHeight", modal_elem)
    print('last_height')
    print(last_height)
 while True:

    # Scroll down to the bottom.
    self.execute_script("arguments[0].scrollTo(0, arguments[0].scrollHeight);", modal_elem)
    
    # Wait to load the page.
    time.sleep(2)
    
    # Calculate new scroll height and compare with last scroll height.
    new_height = self.execute_script("return arguments[0].scrollHeight", modal_elem)
    print('new_height')
    print(new_height)
    if new_height == last_height:

        break

    last_height = new_height
    
 scroll_down(browser)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.