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
        time.sleep(wait_time)

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

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

That's what I use, but here's some other interesting solutions! https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python

SNAPPED!

SNAPPED!

Comments

Popular posts from this blog

Append to a json file python

Setup Mailhog mail server on Ubuntu