User Agents

Web browsers and everyday web tech, explained

Firefox Headless Screenshot Consistent Dimensions

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

Ensuring consistent screenshot dimensions in Firefox when running in headless mode is essential for reliable visual testing, automated reporting, and UI regression checks. By default, Firefox headless uses a viewport size of 800x600 pixels, but this can vary depending on system settings, environment variables, or WebDriver configurations, leading to inconsistent outputs across runs. This guide shows how to lock down the viewport and output dimensions so every screenshot matches exactly in size and content, regardless of environment.

What You'll Need

  • Firefox browser (version 56 or later)
  • GeckoDriver (latest compatible version)
  • Automation tool: Selenium WebDriver (Python, JavaScript, Java, etc.) or Puppeteer (via Firefox support)
  • Basic knowledge of command-line arguments or WebDriver options
  • A script or test environment to trigger screenshots

Step-by-Step

  1. Install and configure GeckoDriver: Download the correct version of GeckoDriver for your system from the official Mozilla GitHub repository. Ensure it’s in your system PATH or specify its location explicitly in your automation script.
  2. Launch Firefox with fixed window size: Use command-line arguments or WebDriver options to set a specific window size. In headless mode, Firefox doesn’t respect CSS media queries or responsive layouts unless the viewport is explicitly defined. Use --width and --height arguments to define the viewport dimensions. For example: --width=1920 --height=1080.
  3. Set window size via WebDriver: Even with command-line flags, some environments override these. In your automation script, explicitly set the window size using the WebDriver API. For example, in Python with Selenium:
    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument("--headless")
    options.add_argument("--width=1920")
    options.add_argument("--height=1080")
    
    driver = webdriver.Firefox(options=options)
    driver.set_window_size(1920, 1080)
    This double-enforcement ensures consistency.
  4. Avoid implicit resizing from page content: Some web pages trigger JavaScript that resizes the viewport or adjusts layout based on available space. To prevent this, ensure the target page does not use window.resizeTo() or responsive scripts that alter rendering. If you control the page, disable such scripts during testing.
  5. Use a fixed device pixel ratio (DPR): Firefox headless may report different DPR values depending on the system, affecting how CSS pixels map to physical pixels. To standardize this, launch Firefox with --force-device-scale-factor=1 (if supported) or ensure your test environment uses the same display scaling. While Firefox doesn’t support this flag natively like Chrome, running in a controlled environment (e.g., Docker with fixed DPI) helps.
  6. Take screenshots using consistent methods: Use driver.save_screenshot('output.png') or equivalent in your language. Avoid full-page screenshots unless necessary, as they can vary in height based on content. If you need full-page captures, scroll and stitch manually with fixed viewport sizes to maintain control.
  7. Validate output dimensions programmatically: After capturing, verify the image dimensions using an image library (e.g., Pillow in Python). If the size is incorrect, log the discrepancy and adjust your setup. Example:
    from PIL import Image
    img = Image.open('output.png')
    print(img.size)  # Should be (1920, 1080)
  8. Run in a controlled environment: Use Docker or virtual machines with fixed screen settings to eliminate host system variability. For example, set MOZ_HEADLESS_WIDTH and MOZ_HEADLESS_HEIGHT environment variables if supported in your Firefox version.

Common Mistakes

  • Assuming headless mode uses the same viewport as headed mode — it does not; always set dimensions explicitly.
  • Only setting window size in the script but not via command-line arguments, leading to race conditions during startup.
  • Using maximize_window() in headless mode, which has no effect and can cause undefined behavior.
  • Ignoring system-level display scaling, which may affect rendering even in headless mode when not fully isolated.
  • Taking screenshots before the page is fully loaded, resulting in incomplete or inconsistently rendered content.

Tips

  • Always wait for document.readyState === 'complete' before capturing screenshots to ensure full rendering.
  • Use explicit waits for dynamic content (e.g., images, AJAX) to avoid timing-related inconsistencies.
  • Standardize Firefox versions and GeckoDriver across all environments to prevent rendering differences.
  • Store your screenshot settings (width, height, headless flag) in a configuration file for reuse and consistency.
  • Test your setup on multiple machines or CI/CD runners to confirm cross-environment stability.

Troubleshooting

If screenshots still vary in size, first check the actual viewport during execution. Inject JavaScript to log the window.innerWidth and window.innerHeight values right before the screenshot. If they don’t match your intended size, the issue is in Firefox startup configuration. Ensure no conflicting arguments are passed. If using Selenium, verify that options are applied before driver initialization. If running in CI, check for missing display settings — some Linux containers require xvfb even in headless mode. Lastly, update Firefox and GeckoDriver to the latest compatible versions, as older builds had bugs in headless viewport handling.

Frequently Asked Questions

Does Firefox headless respect CSS media queries?

Yes, but only if the viewport size is explicitly set. Otherwise, it defaults to 800x600, which may not reflect your target device.

Can I capture screenshots larger than 1920x1080?

Yes. Firefox supports custom viewport sizes. Set --width and --height to your desired resolution, but ensure your system can render it.

Why are my screenshots blurry or low-resolution?

This may be due to high device pixel ratio. Run in a clean environment with scaling set to 100% or use a container with fixed DPI settings.

Do I need a display server for Firefox headless?

No. Headless mode runs without a display, but some CI environments still require xvfb for compatibility with older libraries.

Can I automate this with Puppeteer?

Limited support exists via puppeteer-firefox, but official Puppeteer only supports Chrome. Use Selenium for reliable Firefox automation.

Are full-page screenshots consistent in headless mode?

They can be, but height varies with content. For consistency, use fixed viewports and scroll-stitch logic with defined scroll intervals.