Full Page Screenshots with Selenium and Chrome Guide
In this article
Capturing a full-page screenshot with Selenium and Chrome allows you to save the entire length of a webpage, including content that requires scrolling, into a single image file. By default, the standard save_screenshot method in Selenium only captures the current viewport (the visible area of the browser). To capture the full page, you must use Chrome DevTools Protocol (CDP) commands to calculate the page height and resize the browser window accordingly, or use the Page.captureScreenshot command to render the entire DOM.
What You'll Need
- Python installed on your system.
- Selenium WebDriver library (
pip install selenium). - Google Chrome browser installed.
- ChromeDriver that matches your installed version of Chrome.
Step-by-Step
- Initialize the WebDriver: Start by importing the necessary modules and setting up your Chrome options. It is highly recommended to run the browser in
--headlessmode. Headless mode is essential for full-page screenshots because it allows the browser to resize the window to the actual height of the page content without being limited by the physical resolution of your monitor. - Navigate to the URL: Use
driver.get("your_url_here")to load the target webpage. Wait for the page to load completely. If the page uses lazy-loading images or dynamic content, you may need to implement a script that scrolls to the bottom of the page first to trigger all asset loads. - Determine Page Dimensions: You need to find the total height of the body element. You can achieve this by executing a JavaScript snippet via Selenium:
height = driver.execute_script("return document.body.parentNode.scrollHeight"). This returns the total height of the document in pixels. - Set the Window Size: Use
driver.set_window_size(width, height). Set the width to your preferred resolution (e.g., 1920 pixels) and the height to the value retrieved in the previous step. This forces the headless browser to render the entire page as one long continuous view. - Execute the Screenshot: Now that the window size matches the page size, call
driver.save_screenshot("full_page.png"). Because the window is now as tall as the content, the resulting image will cover the entire page from header to footer. - Close the Session: Always call
driver.quit()to close the browser process and free up system memory.
Common Mistakes
- Forgetting Headless Mode: Attempting to resize a visible browser window to 10,000 pixels in height often fails or behaves unpredictably because the OS limits the window size to the screen resolution.
- Ignoring Lazy Loading: Many modern sites only load images as you scroll. If you take a screenshot immediately after loading the page, you will see empty placeholders instead of images.
- Incorrect Driver Version: Using a ChromeDriver version that does not match the Chrome browser version often leads to session crashes or failure to execute CDP commands.
- Hardcoding Dimensions: Setting a fixed height (e.g., 3000px) instead of dynamically calculating the
scrollHeightleads to cropped images on long pages or excessive whitespace on short pages.
Tips
- Scroll to Trigger Content: To handle lazy-loading, use a
forloop to scroll down the page in increments of 500 pixels, pausing briefly between each jump before calculating the final page height. - Use CDP for High Resolution: For even more control, use
driver.execute_cdp_cmd('Page.captureScreenshot', {'format': 'png', 'fromSurface': True}). This interacts directly with the Chrome DevTools Protocol and can sometimes produce cleaner results than the standard Selenium method. - Disable Animations: Use JavaScript to disable CSS animations or transitions before taking the shot to prevent "ghosting" or half-animated elements from appearing in the image.
Troubleshooting
If your screenshots are coming out blank or only capturing the top fold, first verify that you are using --headless=new in your ChromeOptions. Older versions of headless Chrome handled window sizing differently than the current implementation.
If the image is cut off at the bottom, check if the page has a "sticky" footer or a complex layout where the body height differs from the documentElement height. Try using Math.max(document.body.scrollHeight, document.documentElement.scrollHeight) in your JavaScript snippet to ensure you are getting the absolute maximum height.
If you encounter a SessionNotCreatedException, update your ChromeDriver to the latest version or use the webdriver-manager library to automate the version matching process.
Frequently Asked Questions
Can I take full-page screenshots in non-headless mode?
It is technically possible but unreliable. Most operating systems prevent the browser window from expanding beyond the physical screen height, meaning the screenshot will be clipped.
How do I handle pages with sticky headers?
Sticky headers will appear once at the top of the screenshot. However, if they overlap content, you may need to use JavaScript to hide the header element (display: none) before capturing the image.
Will this capture iframes?
Yes, as long as the iframe content has loaded. However, if the iframe has its own internal scrollbar, only the visible portion of that iframe will be captured.
What is the best file format for these screenshots?
PNG is the best choice as it is a lossless format, ensuring that text remains sharp and readable, which is critical for long-form page captures.
Does this work on mobile views?
Yes. You can use Chrome DevTools emulation via Selenium to set a mobile device profile (like an iPhone or Pixel) and then follow the same height-calculation steps.