import time from datetime import date from time import sleep import numpy as np import logging as log def infinite_scoll(driver, module, log): print("Infinite scrolling...") """A method for scrolling the page.""" last_height = driver.execute_script("return document.body.scrollHeight") while True: driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") print("Scrolled a page") # Wait to load the page. rand_pause = np.random.normal(5.0, 1.0) time.sleep(rand_pause) print(rand_pause) # Calculate new scroll height and compare with last scroll height. new_height = driver.execute_script("return document.body.scrollHeight") if new_height == last_height: break last_height = new_height print("Infinite scroll finished, screen grabbed") driver.save_screenshot("%s-scroll.png" % (module)) def scroll_to_bottom(): """Scroll to the bottom of the page Params: - scroll_pause_time {float}: time to wait (s) between page scroll increments - scroll_increment {int}: increment size of page scrolls (pixels) """ # NOTE: this starts scrolling from the current scroll position, not the top of the page. current_height = driver.execute_script("return document.documentElement.scrollTop") print(current_height) while True: # click_expandable_buttons() # Scroll down to bottom in increments of self.scroll_increment new_height = driver.execute_script( "return Math.min({}, document.body.scrollHeight)".format( current_height + scroll_increment ) ) if new_height == current_height: break # driver.execute_script( # "window.scrollTo(0, {});".format(new_height)) # JavascriptExecutor js = (JavascriptExecutor) driver; # js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); current_height = new_height print(current_height) # Wait to load page time.sleep(scroll_pause)