User Agents

Web browsers and everyday web tech, explained

Full Page Screenshots with Selenium in Headed Chrome

· 6 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Capturing a full-page screenshot in headed Chrome using Selenium requires a specific approach because the standard save_screenshot method only captures the current viewport (the visible area of the browser window). To capture the entire length of a webpage, you must programmatically determine the total height of the page's body and resize the browser window to match those dimensions before triggering the capture. This process ensures that every element from the header to the footer is rendered and included in the final image file.

What You'll Need

  • Python installed on your system (version 3.7 or higher recommended).
  • The Selenium library installed via pip (pip install selenium).
  • Google Chrome browser installed.
  • ChromeDriver that matches your current version of Chrome.
  • A basic understanding of Python scripting and how to handle file paths.

Step-by-Step

  1. Initialize the WebDriver: Start by importing the necessary modules from Selenium. You will need webdriver and ChromeOptions. Since you are running in headed mode, you do not need to add the --headless argument, meaning the browser window will physically open on your desktop.
  2. Navigate to the Target URL: Use the driver.get("URL") method to load the page you wish to capture. It is critical to wait for the page to load completely. For pages with heavy JavaScript or lazy-loading images, you should implement an explicit wait using WebDriverWait to ensure the DOM is fully populated.
  3. Calculate the Total Page Height: Because Chrome does not have a native "full page" command in headed mode, you must ask the browser for the dimensions of the element. You can achieve this by executing a JavaScript snippet through Selenium. Use driver.execute_script("return document.body.parentNode.scrollHeight"). This returns the total height of the HTML document in pixels, regardless of the current window size.
  4. Set the Window Size: Once you have the height, you need to resize the browser window. Use driver.set_window_size(width, height). For the width, you can use the current window width or a standard desktop width (e.g., 1920 pixels). For the height, pass the value you retrieved from the JavaScript execution in the previous step. This forces the browser to expand the viewport to encompass the entire page.
  5. Handle Lazy-Loading Content: Many modern websites only load images or content as the user scrolls down. If you simply resize the window, these elements may remain blank. To fix this, write a small loop that scrolls the page down in increments (e.g., 500 pixels at a time) until it reaches the bottom, then scrolls back to the top. This triggers all lazy-load events.
  6. Capture the Screenshot: With the window resized and all content loaded, call driver.save_screenshot("full_page.png"). Selenium will now capture the entire expanded viewport, resulting in a long vertical image of the complete page.
  7. Close the Session: Always end your script with driver.quit() to close the browser window and free up system resources.

Common Mistakes

One of the most frequent errors is failing to account for the difference between document.body.scrollHeight and document.documentElement.scrollHeight. Depending on the CSS of the website (specifically if the height is set on the body or the html tag), one may return a smaller value than the other. To be safe, always use the maximum of both or target the parentNode of the body.

Another common mistake is neglecting the device pixel ratio (DPR). On high-resolution screens (like Mac Retina displays), the browser reports coordinates in "CSS pixels," but the actual screenshot is captured in physical pixels. This can lead to screenshots that are twice as large as expected or slightly cropped. To handle this, you may need to multiply your calculated height by the window.devicePixelRatio via JavaScript.

Many users also forget to handle "sticky" elements. Headers or navigation bars that use position: fixed will be captured repeatedly or may overlap content in strange ways when the viewport is artificially expanded. While this is a limitation of the screenshot method, it is important to be aware that the resulting image is a literal render of the expanded window.

Tips

To improve the quality of your captures, consider using a consistent window width. Setting the width to 1920px ensures that you are capturing the desktop version of the site and avoids triggering mobile responsive layouts that might change the page height.

If you are dealing with pages that have dynamic content (like infinite scrolls), you will need to implement a while loop that continues to scroll and check the height until the height stops increasing. Only after the page has reached its absolute limit should you perform the final window resize and screenshot.

For better organization, use the os module in Python to create a timestamps-based filename. This prevents your script from overwriting previous screenshots when running the process in a loop across multiple URLs.

Troubleshooting

The screenshot is truncated or cut off: This usually happens because the scrollHeight was calculated before the page finished rendering. Increase your wait time or use time.sleep() as a quick test to see if the page needs more time to expand. Additionally, check if the page uses a nested scrollable div instead of the main body; if it does, you must calculate the height of that specific element instead of the body.

Images are missing or blank: This is a classic sign of lazy-loading. Ensure your script scrolls to the bottom of the page before taking the shot. If images still don't appear, check if the site has anti-bot protections that block requests from automated drivers; you may need to modify your User-Agent string to appear more like a human user.

The browser window doesn't resize: Ensure you are not running the browser in a restricted environment (like some virtual machines or containers) that limits the maximum window size. Some OS environments cap the height of a window, which will prevent Selenium from expanding it to 10,000+ pixels for very long pages.

Frequently Asked Questions

Can I do this without the browser window popping up?

Yes, by adding the --headless argument to ChromeOptions. However, the process of calculating height and resizing remains the same, as headless Chrome still simulates a viewport.

Does this work for PDFs?

No, save_screenshot produces image files (PNG/JPG). To get a PDF, you should use the Chrome DevTools Protocol (CDP) command Page.printToPDF, which is a separate functionality from standard screenshots.

Why is my screenshot blurry?

Blurriness often occurs due to the Device Pixel Ratio. If you are on a 4K or Retina screen, the screenshot may be scaled. Using a headless browser often standardizes the resolution and can lead to cleaner, more consistent results.

How do I handle pop-ups or cookie banners?

You must programmatically find the "Accept" or "Close" button using find_element and click() before calculating the page height. Otherwise, the banner will obstruct the content in your final image.

Is there a limit to how long the page can be?

While there is no hard limit in Selenium, extremely long pages (e.g., 20,000+ pixels) may cause the browser to crash or the image file to exceed the maximum allowed dimensions for a PNG, leading to a corrupted file.