Infinite scroll selenium python

One way to handle infinite scroll is by using the height of the viewport. Here we continue to scroll until the viewport height before and after does not change, hence, all elements on the page have been loaded

import time

def scroll_infinite(driver, wait_time, number_of_times_to_scroll):
    # Current scroll height
    current_height = driver.execute_script("return document.body.scrollHeight")
    while True and number_of_times_to_scroll > 0:
        # Scroll to bottom of page
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        # Wait for new content to load

        # Calculate new scroll height
        n_height = driver.execute_script("return document.body.scrollHeight")

        if n_height == current_height:
        number_of_times_to_scroll -= 1
        current_height = n_height

That's what I use, but here's some other interesting solutions!




Popular posts from this blog

Append to a json file python

Setup Mailhog mail server on Ubuntu