Take screenshot of full page with Selenium Python with chromedriver Take screenshot of full page with Selenium Python with chromedriver python python

Take screenshot of full page with Selenium Python with chromedriver


Screenshots are limited to the viewport but you can get around this by capturing the body element, as the webdriver will capture the entire element even if it is larger than the viewport. This will save you having to deal with scrolling and stitching images, however you might see problems with footer position (like in the screenshot below).

Tested on Windows 8 and Mac High Sierra with Chrome Driver.

from selenium import webdriverurl = 'https://stackoverflow.com/'path = '/path/to/save/in/scrape.png'driver = webdriver.Chrome()driver.get(url)el = driver.find_element_by_tag_name('body')el.screenshot(path)driver.quit()

Returns: (full size: https://i.stack.imgur.com/ppDiI.png)

SO_scrape


This answer improves upon prior answers by am05mhz and Javed Karim.

It assumes headless mode, and that a window-size option was not initially set. Before calling this function, ensure the page has loaded fully or sufficiently.

It attempts to set the width and height both to what is necessary. The screenshot of the entire page can sometimes include a needless vertical scrollbar. One way to generally avoid the scrollbar is by taking a screenshot of the body element instead. After saving a screenshot, it reverts the size to what it was originally, failing which the size for the next screenshot may not set correctly.

Ultimately this technique may still not work perfectly well for some examples.

from selenium import webdriverdef save_screenshot(driver: webdriver.Chrome, path: str = '/tmp/screenshot.png') -> None:    # Ref: https://stackoverflow.com/a/52572919/    original_size = driver.get_window_size()    required_width = driver.execute_script('return document.body.parentNode.scrollWidth')    required_height = driver.execute_script('return document.body.parentNode.scrollHeight')    driver.set_window_size(required_width, required_height)    # driver.save_screenshot(path)  # has scrollbar    driver.find_element_by_tag_name('body').screenshot(path)  # avoids scrollbar    driver.set_window_size(original_size['width'], original_size['height'])

If using Python older than 3.6, remove the type annotations from the function definition.


How it works: set browser height as longest as you can...

#coding=utf-8import timefrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsdef test_fullpage_screenshot(self):    chrome_options = Options()    chrome_options.add_argument('--headless')    chrome_options.add_argument('--start-maximized')    driver = webdriver.Chrome(chrome_options=chrome_options)    driver.get("yoururlxxx")    time.sleep(2)    #the element with longest height on page    ele=driver.find_element("xpath", '//div[@class="react-grid-layout layout"]')    total_height = ele.size["height"]+1000    driver.set_window_size(1920, total_height)      #the trick    time.sleep(2)    driver.save_screenshot("screenshot1.png")    driver.quit()if __name__ == "__main__":    test_fullpage_screenshot()