User Agents

Web browsers and everyday web tech, explained

Fix White Screenshots in Headless Chrome Selenium

· 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

White screenshots and missing elements in headless Chrome with Selenium usually happen because the browser window has no defined size, the page hasn't finished loading, or GPU rendering is disabled. This guide shows you how to fix those issues step by step.

What You'll Need

  • Python 3.6+ with Selenium installed (pip install selenium)
  • Chrome browser and matching ChromeDriver
  • Basic knowledge of Selenium WebDriver

Step-by-Step

  1. Set a real window size before starting headless mode. Headless Chrome defaults to a small viewport (800x600), which can cause elements to be off-screen or not rendered. Use --window-size=1920,1080 in your Chrome options. Example:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--window-size=1920,1080')
    options.add_argument('--force-device-scale-factor=1')
    driver = webdriver.Chrome(options=options)
  2. Disable GPU acceleration. Headless Chrome often has GPU issues that cause blank screenshots. Add --disable-gpu to your options. This is especially important on Windows and older Linux systems.
  3. Use the correct screenshot method. Instead of driver.save_screenshot(), try element.screenshot() for specific elements, or use driver.get_screenshot_as_png() if you need the raw bytes. For full-page screenshots, you may need to manually resize the window to the page height.
  4. Wait for the page to fully load. Use driver.implicitly_wait(10) or better, explicit waits with WebDriverWait for elements you expect to appear. Example:
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 10)
    wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
  5. Scroll to the element before capturing. If an element is below the fold, it may not appear in the screenshot. Use driver.execute_script("arguments[0].scrollIntoView();", element) before taking the shot.
  6. Set a high DPI scale factor. Sometimes the default scale causes blurry or missing text. Add --force-device-scale-factor=1 to keep the rendering at 100%.
  7. Use the 'new' headless mode (Chrome 112+). The old headless mode is being phased out. Use --headless=new to enable the new implementation, which behaves more like regular Chrome and fixes many rendering issues.
  8. Disable background throttling. Headless Chrome may throttle timers and animations. Add --disable-background-timer-throttling and --disable-backgrounding-occluded-windows to keep the page active.
  9. Use a virtual display if on Linux. If you're on a headless Linux server, install Xvfb and run your script under it. This gives Chrome a real display to render to, even if you're still using headless mode. Example: xvfb-run python your_script.py
  10. Check for iframes and shadow DOM. Elements inside iframes require switching context with driver.switch_to.frame(). Shadow DOM elements need JavaScript to access them. If screenshots show blanks where those elements should be, it's not a rendering issue—it's a locator issue.

Common Mistakes

  • Forgetting to set window size—this is the #1 cause of white screenshots.
  • Using --disable-gpu on all platforms, even where it's not needed (it's harmless, but not always the fix).
  • Not waiting for the page to load before taking a screenshot.
  • Taking a screenshot of a specific element but not scrolling it into view first.
  • Using an outdated ChromeDriver version that doesn't match your Chrome version.
  • Assuming the page is fully rendered just because the URL is loaded—many sites use JavaScript that takes time to execute.

Tips

  • Always use the latest ChromeDriver and Chrome versions to avoid compatibility issues.
  • Test your script in non-headless mode first to see if the problem only occurs in headless.
  • Use driver.get_screenshot_as_png() and write to a file manually—it sometimes works when save_screenshot fails.
  • If you need to capture a full page, set the window size to the page's scroll height before capturing.
  • For dynamic content, use explicit waits with expected conditions rather than fixed sleeps.
  • Consider using --hide-scrollbars to avoid blank areas where scrollbars would appear.

Troubleshooting

If you still get white screenshots after following all steps:

  • Check the ChromeDriver logs for any errors. Run your script with service = webdriver.chrome.service.Service(executable_path='chromedriver', service_args=['--verbose', '--log-path=chromedriver.log']) to see what's happening.
  • Try adding --no-sandbox if you're running as root or in a container (common in CI environments).
  • Try --disable-dev-shm-usage on Linux to avoid shared memory issues.
  • If the page uses WebGL or canvas, you may need to enable software rendering with --enable-unsafe-swiftshader.
  • If elements are missing but the background isn't white, the issue might be with CSS animations or lazy loading. Use --disable-animations or wait for the specific element to be visible.

Frequently Asked Questions

Why is my screenshot completely white?

Most commonly because the window size is too small or the page hasn't loaded. Set a larger window size and use explicit waits.

Why are some elements missing from the screenshot?

Elements may be outside the viewport, inside iframes, or rendered via JavaScript that hasn't executed yet. Scroll to them, switch to the iframe, or wait for the JavaScript to run.

Does headless mode affect rendering quality?

Yes, headless mode can use different rendering paths. The new headless mode (--headless=new) is more reliable and recommended.

Can I take a full-page screenshot in headless Chrome?

Yes, by resizing the window to the page height before capturing, or using a tool like Selenium's full-page screenshot (not built-in, but you can do it manually).

Do I need to disable GPU on Linux?

Often yes, especially on servers without a GPU. Adding --disable-gpu is safe and can fix many issues.

Why do screenshots work in normal mode but not headless?

Headless mode has different defaults and may not support certain features like hardware acceleration. The steps above help align headless behavior with normal mode.