Full Page Screenshots with Selenium Python: Chrome & Firefox
In this article
Capturing a full-page screenshot with Selenium in Python requires bypassing the default viewport limitation, as standard screenshot methods only capture the visible area of the browser window. To achieve a complete capture of a webpage from header to footer, 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 command. This process ensures that all elements, including those below the fold, are rendered and included in the final image file.
What You'll Need
- Python 3.x installed on your system.
- The Selenium library (
pip install selenium). - WebDriver for your chosen browser (ChromeDriver for Chrome or GeckoDriver for Firefox).
- The browser itself installed and updated to a version compatible with your WebDriver.
- A basic understanding of Python scripting and how to handle file paths.
Step-by-Step
- Initialize the WebDriver: Start by importing the necessary modules from Selenium. You will need
webdriverfor browser control,Serviceto manage the driver executable, andOptionsto configure headless mode. Headless mode is critical for full-page screenshots because it allows the browser to resize to arbitrary dimensions without being constrained by the physical resolution of your monitor. - Configure Headless Mode: Create an options object for your specific browser. For Chrome, use
options.add_argument('--headless'). For Firefox, useoptions.add_argument('-headless'). Without headless mode, the browser window cannot expand beyond your screen size, which often results in the screenshot being cut off or failing to capture the full vertical length of the document. - Navigate to the Target URL: Use the
driver.get("url")method to load the webpage you wish to capture. Ensure the page has fully loaded before proceeding to the measurement step. For pages with heavy JavaScript or lazy-loading images, you may need to implement aWebDriverWaitto ensure specific elements are present. - Calculate the Page Height: This is the most critical technical step. You must execute a JavaScript snippet via Selenium to find the total height of the
bodyordocumentElement. Use the following logic:height = driver.execute_script("return document.body.scrollHeight"). This returns the height of the entire page in pixels, regardless of what is currently visible in the viewport. - Resize the Window: Once you have the height, use
driver.set_window_size(width, height). The width can be a standard desktop resolution (e.g., 1920 pixels), while the height should be the value retrieved in the previous step. By forcing the window to be as tall as the content, you effectively eliminate the scrollbar and make the entire page "visible" to the browser's rendering engine. - Capture the Screenshot: Call the
save_screenshot("filename.png")method. Because the window is now the size of the full page, Selenium will capture the entire rendered area. - Close the Session: Always call
driver.quit()to close the browser process and free up system memory.
Common Mistakes
One of the most frequent errors is attempting to take a full-page screenshot in non-headless mode. When the browser is visible, the operating system often prevents the window from expanding beyond the physical screen boundaries. This leads to screenshots that only capture the first 1080 pixels (or whatever your monitor height is), even if the set_window_size command was issued. Always use headless mode for automated full-page captures.
Another common mistake is ignoring lazy-loading content. Many modern websites only load images or data as the user scrolls down. If you simply resize the window and take a screenshot, the bottom half of the page may contain empty placeholders or loading spinners instead of actual content. To solve this, you must script a "virtual scroll"—a loop that scrolls the page down in increments, waits a fraction of a second, and then scrolls further—before calculating the final height and taking the shot.
Developers also frequently forget to handle the viewport meta tag. Some sites have fixed viewport settings that can interfere with how Selenium interprets the page width. If your screenshots look zoomed in or strangely narrow, you may need to use JavaScript to temporarily modify the viewport settings or set a very specific width in your set_window_size call.
Tips
For higher quality images, consider using the Chrome DevTools Protocol (CDP). Chrome allows you to send a command called Page.captureScreenshot with a parameter captureBeyondViewport: true. This is more robust than resizing the window because it tells the browser engine to render the full page internally, avoiding some of the glitches associated with window resizing.
To handle dynamic content, integrate the time.sleep() function or WebDriverWait. If a page has animations (like fade-ins) that trigger on scroll, a screenshot taken immediately after resizing will capture the page in a state of transition. Giving the page 2-3 seconds to "settle" after the final scroll ensures a professional-looking image.
If you are capturing many pages, use a dynamic naming convention for your files. Instead of screenshot.png, use a timestamp or the page title: f"{page_title}_{timestamp}.png". This prevents files from being overwritten and makes organizing your data much easier.
Troubleshooting
The image is blank or white: This usually happens if the screenshot is triggered before the page has finished rendering. Increase your wait times or use expected_conditions to wait for a specific element at the bottom of the page to be visible before taking the shot.
The screenshot is cut off at the bottom: This occurs when the scrollHeight is calculated before the page has fully expanded (common with AJAX content). Try scrolling to the bottom of the page first, then calculating the height, then resizing the window, and finally taking the screenshot. This ensures the browser knows the true final dimensions of the document.
The image resolution is too low: Check your device pixel ratio. On high-DPI screens (like Retina displays), the browser may capture at a different scale. You can force a specific device scale factor using Chrome Options: options.add_argument('--force-device-scale-factor=1') to ensure consistent output across different machines.
WebDriver executable not found: Ensure that the path to your chromedriver or geckodriver is correctly specified in the Service object, or that the driver is added to your system's PATH environment variable. If you are using a recent version of Selenium (4.6+), the Selenium Manager should handle this automatically, but manual pathing is still a reliable fallback.
Frequently Asked Questions
Can I capture full pages in non-headless mode?
Generally, no. Most operating systems limit the maximum height of a window to the screen's resolution. Headless mode removes this restriction, allowing the virtual window to be thousands of pixels tall.
Does this work for both Chrome and Firefox?
Yes, the logic of calculating scrollHeight and resizing the window is universal to most modern browsers. The only differences are the specific Options classes used to enable headless mode.
How do I handle pages with sticky headers?
Sticky headers can appear multiple times in a full-page screenshot if you use a "stitching" method (taking multiple small shots). However, using the set_window_size method treats the page as one long canvas, so the sticky header will only appear once at the top.
What is the best file format for these screenshots?
PNG is the recommended format because it is lossless. Since full-page screenshots can be very large, JPEG might introduce compression artifacts around text, making the screenshot harder to read.
Why is my screenshot showing a scrollbar on the right?
This happens if the window width is slightly smaller than the page content. To avoid this, set your window width to be slightly larger than the document.body.scrollWidth or use a standard width like 1920 pixels.
Can I save the screenshot as a PDF instead?
Selenium's save_screenshot only supports images. To save as a PDF, you must use the browser's print-to-PDF functionality via Chrome DevTools Protocol (CDP) commands, which is a separate process from image capturing.