User Agents

Web browsers and everyday web tech, explained

Fix Selenium Firefox Screenshot Wrong Height

· 4 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

When you take a screenshot with Selenium WebDriver in Firefox, the image sometimes comes out with the wrong height — usually shorter than the full page, or cut off at the viewport. This happens because Firefox's built-in screenshot command captures only the visible area by default, and the common workarounds (like resizing the window) don't always work reliably. This guide shows you how to capture full-page screenshots with correct height in Selenium using Firefox, including handling pages with dynamic content and sticky elements.

What You'll Need

  • Selenium WebDriver installed (Python, Java, or C# — the concept is the same)
  • geckodriver installed and on your PATH
  • Firefox browser (recent version)
  • Basic knowledge of your Selenium language bindings

Step-by-Step

  1. Identify the root cause. The default driver.save_screenshot() or driver.get_screenshot_as_file() captures only the viewport in Firefox. To get the full page, you need to either use a JavaScript-based approach or adjust the window size to the full document height before capturing.
  2. Get the full document height. Use JavaScript to retrieve the scroll height of the page. In Python, this looks like:
    height = driver.execute_script("return document.body.scrollHeight")
    For pages with nested scroll containers, also check document.documentElement.scrollHeight and take the maximum.
  3. Resize the window to the full height. Set the browser window size to the page width and full height. For example:
    driver.set_window_size(width, height)
    Use the page's actual width (from window.innerWidth) to avoid horizontal scrollbars.
  4. Wait for the page to settle. After resizing, wait a moment for the layout to reflow. A simple time.sleep(1) or an explicit wait for a condition works.
  5. Take the screenshot. Now call your screenshot method. It should capture the entire page because the viewport matches the full document height.
  6. Restore the window size. If you need to continue interacting with the page, resize back to your original viewport size to avoid unexpected behavior.
  7. Alternative: Use a scrolling capture method. If resizing fails (e.g., due to fixed headers or dynamic content), implement a scroll-and-stitch approach: take multiple viewport screenshots at different scroll positions and combine them with an image library like Pillow (Python) or BufferedImage (Java). This is more complex but handles most edge cases.
  8. For Selenium 4+ with Firefox. Selenium 4 introduced a FullPageScreenshot feature, but it's only supported in Chrome and Edge, not Firefox. So you must use the resize or scroll method for Firefox.

Common Mistakes

  • Using document.body.scrollHeight on pages with a doctype. Some pages use document.documentElement.scrollHeight instead. Always take the maximum of both.
  • Not accounting for the browser's scrollbar width. When you resize the window, the page width may change because a scrollbar appears/disappears. Set the width to document.documentElement.clientWidth to avoid this.
  • Taking the screenshot before the page finishes loading. Use driver.implicitly_wait() or explicit waits for elements that affect height.
  • Forgetting to restore the window size. This can cause subsequent tests to fail because the viewport is huge.

Tips

  • If you're using Python, consider the selenium-wire or pytest-selenium plugins that have built-in full-page screenshot helpers, but they still rely on the same principles.
  • For pages with infinite scroll, you may need to scroll to the bottom first to load all content, then get the final height.
  • Use a headless Firefox with a fixed window size — sometimes headless mode handles resizing better.
  • If your page has fixed elements (like a sticky header), the screenshot may still include them correctly, but the scroll method might duplicate them. The resize method avoids that.

Troubleshooting

  • Screenshot still cut off. Try using document.documentElement.scrollHeight instead of document.body. Also, check if the page has a min-height on the body.
  • Window resize doesn't work. Some Firefox versions ignore set_window_size in headless mode. Try using the --width and --height options when launching Firefox via FirefoxOptions.
  • Image is too tall and memory runs out. For extremely long pages, the scroll-and-stitch method is more memory-efficient than resizing the window to 50,000 pixels.
  • Blank areas in the screenshot. This often happens when the page uses lazy loading. Scroll to the bottom first, wait for images to load, then capture.
  • Inconsistent heights across runs. Dynamic content (e.g., ads, animations) can change the height. Use explicit waits for elements that affect layout, or disable animations in the page.

Frequently Asked Questions

Why does Firefox only capture the visible part by default?

Firefox's native screenshot command in Selenium is designed to capture the viewport, unlike Chrome's full-page option. This is a limitation of the geckodriver implementation.

Can I use the same method for Chrome and Edge?

Yes, but Chrome and Edge have a built-in FullPageScreenshot in Selenium 4, so you don't need to resize. For older versions, the resize method works too.

What if the page height is dynamic and changes after I resize?

You can take the screenshot immediately after resizing, but if content loads asynchronously, wait for a specific element or a network idle condition before resizing.

How do I combine multiple screenshots for a full page?

Scroll to each position, take a screenshot, and stitch them together using an image library. Ensure you overlap a few pixels to avoid gaps, and handle fixed elements by hiding them if needed.

Does headless Firefox behave differently?

Yes, headless mode may have a default viewport size that is different from your screen. Set the window size explicitly in your options.

Is there a way to get full-page screenshots without resizing?

Yes, you can use external tools like `screenshot` command in Firefox's devtools protocol, but that's not directly available in Selenium. The scroll-and-stitch method is the most common alternative.