User Agents

Fix Chrome Command Line Screenshots That Fail

Fixing Chrome command line screenshots that fail usually requires correcting the headless mode flag, ensuring the window size is explicitly defined, and verifying that the output path has the correct write permissions. When Chrome is run via the command line for automation or screenshots, it often fails because the browser instance closes before the image is rendered or because the "headless" environment lacks a virtual display buffer, resulting in blank or missing files.

What You'll Need

  • Google Chrome installed on your system.
  • Access to a Terminal (macOS/Linux) or Command Prompt/PowerShell (Windows).
  • Administrative permissions to write files to your chosen destination folder.
  • The exact file path to the Chrome executable (chrome.exe on Windows).

Step-by-Step

  1. Locate the Chrome Executable: You must use the absolute path to the Chrome binary. On Windows, this is typically "C:\Program Files\Google\Chrome\Application\chrome.exe". On macOS, it is usually "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome".
  2. Set the Headless Flag: To take a screenshot without opening a visible window, use the --headless flag. For newer versions of Chrome, --headless=new is recommended as it better simulates the actual browser rendering engine.
  3. Define the Window Size: Chrome often defaults to a small or inconsistent window size in headless mode, which can cause screenshots to fail or look distorted. Add --window-size=1920,1080 to your command to ensure a standard desktop resolution.
  4. Specify the Screenshot Command: Use the --screenshot flag followed by the path where you want the image saved. For example: --screenshot="C:\screenshots\page.png".
  5. Disable GPU Acceleration: In many server or command-line environments, the GPU is unavailable, causing the browser to crash before the screenshot is taken. Add --disable-gpu to bypass hardware acceleration.
  6. Execute the Full Command: Combine these flags into a single string.
    Example (Windows): "C:\Program Files\Google\Chrome\Application\chrome.exe" --headless=new --disable-gpu --window-size=1920,1080 --screenshot="C:\temp\screenshot.png" https://www.google.com
  7. Verify the Output: Navigate to your destination folder to ensure the .png file has been created and contains the expected page content.

Common Mistakes

  • Missing Quotes: Forgetting to wrap the path to chrome.exe in double quotes when the path contains spaces (like "Program Files") will cause the command to fail immediately.
  • Incorrect File Extensions: Chrome expects the output path to end in a valid image extension, typically .png. Using .jpg or omitting the extension entirely can lead to errors.
  • Using Old Headless Flags: Using --headless instead of --headless=new in Chrome v109+ can lead to rendering differences where certain elements (like lazy-loaded images) do not appear in the screenshot.
  • Permission Denied: Attempting to save the screenshot directly to the root C: drive or a system-protected folder without running the terminal as an Administrator.

Tips

  • Full Page Screenshots: To capture the entire length of a page rather than just the viewport, use the --hide-scrollbars flag in conjunction with a very large window height (e.g., --window-size=1920,5000).
  • Wait for Page Load: If the screenshot is blank, the page may be loading content via JavaScript. While the basic command line is limited, using a tool like Puppeteer or Playwright provides a waitForSelector function to ensure the page is ready.
  • User-Agent Spoofing: Some websites block headless browsers. Add --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" to mimic a real user.

Troubleshooting

If your screenshots are still failing, check the following scenarios:

  • Blank Images: This is usually caused by a lack of a defined window size or the page requiring a login. Ensure --window-size is set and try a public URL to test.
  • Command Not Found: Double-check your path to the Chrome executable. Use the dir (Windows) or ls (macOS/Linux) command to verify the file exists at that specific location.
  • Browser Hangs: If the command line doesn't return to the prompt, Chrome may be stuck in a loop. Use Ctrl+C to kill the process and try adding --no-sandbox (use this only in trusted environments).
  • Incorrect Resolution: If the screenshot looks like a mobile site, it is because the default headless viewport is small. Increase the --window-size values.

Frequently Asked Questions

Why is my screenshot only capturing the top of the page?

The --screenshot flag captures the current viewport. To capture more, you must increase the height in the --window-size flag (e.g., 1920,3000).

Can I take a screenshot of a page that requires a login?

Not easily via a simple command line flag. You would need to specify a user data directory using --user-data-dir="C:\path\to\profile" to use an existing session with saved cookies.

Does this work on Linux servers without a GUI?

Yes, that is exactly what --headless is for. However, you may need to install dependencies like libnss3 and libatk1.0-0 for Chrome to run on a minimal Linux distribution.

What is the difference between --headless and --headless=new?

The original --headless mode used a separate, simplified browser implementation. --headless=new uses the actual Chrome browser engine without a visible window, providing much more accurate rendering.

Can I save the screenshot as a PDF instead?

Yes. Replace the --screenshot flag with --print-to-pdf="C:\path\to\file.pdf".