import random from time import sleep def infinity_scroll_to_the_bottom(driver, scroll_multiplier=1, MAXITER=2000): # Automatically scroll the page scroll_pause_time = 0.1 + random.random() * 2 # Pause between each scroll screen_height = driver.execute_script("return window.screen.height;") # Browser window height i = 1 while i < MAXITER: # Scroll down driver.execute_script(f"window.scrollTo(0, {screen_height * i * scroll_multiplier + random.randint(1, 500)});") i += 1 sleep(scroll_pause_time) # Check if reaching the end of the page scroll_height = driver.execute_script("return document.body.scrollHeight;") # print(scroll_height) if screen_height * i > scroll_height: break return driver