Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to Screenshot Hundreds of Web Pages with Headless Chrome

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

This guide shows you how to capture full-page screenshots of hundreds of web pages automatically using headless Chrome. You'll end up with a simple script that reads a list of URLs, visits each one, and saves a PNG image—no browser window needed. You only need a computer with Chrome installed and basic comfort with the command line.

What You'll Need

  • Google Chrome (version 59 or later) installed on your computer.
  • A text file containing the URLs you want to capture, one per line.
  • Basic familiarity with opening a terminal (Command Prompt on Windows, Terminal on macOS/Linux).
  • Optional: Node.js if you prefer a scripted approach for more control.

Step-by-Step

  1. Create a list of URLs. Open a plain text editor and paste each URL you want to screenshot on its own line. Save the file as urls.txt in a folder you can easily find. Make sure URLs include the full protocol (e.g., https://example.com/page).
  2. Open your terminal. On Windows, press Win + R, type cmd, and press Enter. On macOS, open Spotlight (Cmd + Space) and type Terminal. On Linux, use your system's terminal app.
  3. Navigate to your folder. Use the cd command to move into the directory where you saved urls.txt. For example: cd C:\Users\YourName\screenshots (Windows) or cd /Users/YourName/screenshots (macOS/Linux).
  4. Test headless Chrome with a single URL. Run this command to verify headless mode works (replace the URL and output filename):
    chrome --headless --disable-gpu --screenshot=test.png --window-size=1280,800 https://example.com
    On Windows, you may need to use chrome.exe or the full path to the Chrome executable. If you get an error, see Troubleshooting.
  5. Create a loop for multiple URLs. Instead of typing each command manually, use a loop. On Windows (Command Prompt), create a batch file:
    for /f %i in (urls.txt) do chrome --headless --disable-gpu --screenshot="%i.png" --window-size=1280,800 %i
    On macOS/Linux, use a bash loop:
    while read url; do chrome --headless --disable-gpu --screenshot="$url.png" --window-size=1280,800 "$url"; done < urls.txt
    This will save each screenshot with the URL as the filename (e.g., httpsexample.com.png).
  6. Customize the output names. To avoid unwieldy filenames, use a counter. On Windows:
    setlocal enabledelayedexpansion
    set /a count=0
    for /f %i in (urls.txt) do ( set /a count+=1
    chrome --headless --disable-gpu --screenshot="shot!count!.png" --window-size=1280,800 %i )

    On macOS/Linux:
    count=1; while read url; do chrome --headless --disable-gpu --screenshot="shot$count.png" --window-size=1280,800 "$url"; count=$((count+1)); done < urls.txt
  7. Wait between captures (optional). Some sites may block rapid requests or need time to load. Add a delay of 2-3 seconds between screenshots. On Windows, insert timeout /t 3 /nobreak inside the loop. On macOS/Linux, add sleep 3.
  8. Run the script. Execute the batch file (Windows) or run the bash script (macOS/Linux). Watch the terminal for errors. After it finishes, you'll have one PNG per URL in the same folder.
  9. Check the output. Open a few screenshots to confirm they look correct. If any are blank or missing, see Troubleshooting.

Common Mistakes

  • Forgetting the protocol. URLs must start with http:// or https://; otherwise Chrome treats them as local file paths.
  • Using the wrong Chrome executable name. On Windows, the command might be chrome.exe or the full path like C:\Program Files\Google\Chrome\Application\chrome.exe.
  • Not using --disable-gpu. In headless mode, GPU acceleration can cause errors on some systems. Always include this flag.
  • Overwriting screenshots. If you use the same output name for every URL, you'll only get the last one. Always include a unique identifier.
  • Running out of memory. Capturing hundreds of pages in a single session can consume RAM. If Chrome crashes, try processing in batches of 50 URLs.

Tips

  • Use --window-size to control dimensions. Set width and height in pixels. For full-page screenshots, you may need a larger height or use the --full-page flag (available in newer Chrome versions) to capture the entire scrollable page.
  • Add --virtual-time-budget=5000. This tells Chrome to wait up to 5 seconds after page load for JavaScript to run. Useful for sites with dynamic content.
  • Save to a separate folder. Create an output directory and modify the screenshot path to output/shot1.png to keep things organized.
  • Log errors. Redirect error messages to a file: 2> errors.log on Windows, or 2>> errors.log on macOS/Linux, to see which URLs failed.
  • Use a Node.js script for more control. If you need to handle authentication, cookies, or complex interactions, consider using Puppeteer, which wraps headless Chrome and offers a richer API.

Troubleshooting

  • "Chrome not found" error. Make sure Chrome is installed and the executable is in your PATH. On Windows, try the full path. On macOS, it's usually at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome.
  • Blank screenshots. The page may not have loaded before the capture. Increase the --virtual-time-budget or add a delay. Also check if the site blocks headless browsers (see FAQs).
  • Only one screenshot saved. Your loop likely has a syntax error. Check that the loop variable is correct and that the output filename changes each iteration. Test with a small list of 2-3 URLs first.
  • Chrome crashes with many URLs. Headless Chrome can be memory-intensive. Close other applications, or split your URL list into smaller files and run them sequentially.
  • Pages with login required. Headless Chrome doesn't store cookies by default. You can use the --user-data-dir flag to point to a Chrome profile that has the session cookies, but this is advanced.

Frequently Asked Questions

Can I capture full-page screenshots with headless Chrome?

Yes. Add the --full-page flag to your command. This captures the entire scrollable height of the page, not just the viewport.

Will this work on sites that require JavaScript?

Yes, headless Chrome executes JavaScript like a regular browser. Use --virtual-time-budget to allow time for scripts to run before the screenshot.

How do I handle sites that block headless browsers?

Some sites detect headless Chrome and serve different content. You can try adding --user-agent to set a regular browser user agent, but this is not guaranteed to work. For robust scraping, consider a full browser automation tool like Puppeteer with stealth plugins.

Can I capture screenshots of pages that require a login?

Yes, but you need to provide the session. Use the --user-data-dir flag to point to a Chrome profile that is already logged in. Or, use Puppeteer to automate the login process.

What is the best way to run this on a server?

You can run the same commands on a Linux server. Install Chrome via the package manager, then use the bash loop. For large-scale tasks, consider using a job scheduler or a Node.js script.

Can I save screenshots in other formats?

Headless Chrome saves PNG by default. To save JPEG, use the --screenshot flag with a .jpg extension and add --screenshot-format=jpeg (supported in newer versions).