Smart Systems for Home

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

Screenshot URLs in a Text File with Chrome Headless

· 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 screenshots of every URL listed in a text file using Chrome's headless mode. You'll get a PNG image for each webpage without opening a browser window manually—perfect for batch archiving, visual regression testing, or creating thumbnails. All you need is a computer with Chrome installed and a basic command-line interface (Terminal on macOS/Linux, Command Prompt or PowerShell on Windows).

What You'll Need

  • Google Chrome (or Chromium) installed on your system
  • A text file (e.g., urls.txt) with one URL per line
  • Basic familiarity with opening a terminal or command prompt

Step-by-Step

  1. Prepare your text file. Create a plain text file (e.g., urls.txt) and list each URL on its own line. Include the full protocol (e.g., https://example.com). Save it in a folder you can easily navigate to.
  2. Open your command-line interface. On Windows, press Win + R, type cmd, and press Enter. On macOS, open Terminal from Applications > Utilities. On Linux, use your preferred terminal emulator.
  3. Navigate to the folder containing your text file. Use the cd command to change directories. For example: cd C:\Users\YourName\Desktop (Windows) or cd ~/Desktop (macOS/Linux).
  4. Test Chrome headless on a single URL. Run the following command to verify Chrome works in headless mode. Replace https://example.com with a real URL and test.png with your desired output filename:
    chrome --headless --disable-gpu --screenshot=test.png --window-size=1280,800 https://example.com
    If Chrome is not in your PATH, use the full path (see Common Mistakes).
  5. Write a loop to process all URLs. Use a shell loop to read each line from your text file and run Chrome for each. The exact syntax depends on your operating system.
  6. For Windows (Command Prompt): Create a batch file (e.g., screenshot.bat) with the following content, then run it:
    @echo off
    setlocal enabledelayedexpansion
    set /a count=0
    for /f "tokens=*" %%i in (urls.txt) do (
      set /a count+=1
      chrome --headless --disable-gpu --screenshot=!count!.png --window-size=1280,800 %%i
    )
    echo Done!
    This saves screenshots as 1.png, 2.png, etc.
  7. For macOS/Linux (Bash): Use a while loop. Save the following as screenshot.sh and make it executable with chmod +x screenshot.sh:
    #!/bin/bash
    count=1
    while read url; do
      chrome --headless --disable-gpu --screenshot=$count.png --window-size=1280,800 "$url"
      count=$((count+1))
    done < urls.txt
    Run it with ./screenshot.sh.
  8. Wait for the process to finish. Depending on the number of URLs and your connection speed, this may take a while. Each screenshot is saved in the same directory as your script.
  9. Check the output. Open a few PNG files to confirm they captured the correct pages. If any are blank or missing, refer to the Troubleshooting section.

Common Mistakes

  • Chrome not recognized as a command. On Windows, the executable is often chrome.exe and located in C:\Program Files\Google\Chrome\Application\. Use the full path in quotes, e.g., "C:\Program Files\Google\Chrome\Application\chrome.exe".
  • URLs without protocol. If a line is www.example.com, Chrome may fail. Always include https:// or http://.
  • Blank screenshots due to missing --virtual-time-budget. Some pages load content dynamically. Add --virtual-time-budget=5000 to give the page 5 seconds to render before capturing.
  • Using the wrong flag for the screenshot filename. The flag is --screenshot=filename.png, not --screenshot filename.png.

Tips

  • Use descriptive filenames. Instead of numeric counters, you can use the domain name by parsing the URL. In Bash, you can extract the hostname with echo $url | sed 's|https\?://||' | cut -d/ -f1.
  • Set a custom window size. The --window-size=WIDTH,HEIGHT flag controls the viewport. Use --window-size=1920,1080 for full HD captures, but remember that some pages may look different at larger sizes.
  • Add a delay for slow pages. Use --virtual-time-budget=10000 to wait up to 10 seconds. This is more reliable than a fixed sleep.
  • Capture full-page screenshots. Add --screenshot --full-page (note: the --full-page flag works in recent Chrome versions) to capture the entire scrollable area, not just the visible viewport.
  • Use a separate output directory. Create a folder (e.g., shots) and use --screenshot=shots/!count!.png to keep things organized.

Troubleshooting

  • Chrome opens a normal window instead of headless. Ensure you use --headless (or --headless=new for newer Chrome versions). Also, the --disable-gpu flag is recommended to avoid GPU-related issues.
  • Screenshots are all black or blank. This often happens when the page uses WebGL or requires GPU acceleration. Try adding --disable-software-rasterizer or use --headless=new.
  • Command not found or permission denied. On Linux/macOS, you may need to use google-chrome or chromium-browser instead of chrome. Also ensure the script is executable with chmod +x.
  • Some URLs fail while others succeed. Check the URL for typos or special characters. If a URL contains spaces, wrap it in quotes in the loop (as shown in the Bash example).
  • The loop stops after the first URL. In Windows batch, the for /f loop can be tricky with delayed expansion. Ensure you have setlocal enabledelayedexpansion and use !count! instead of %count% inside the loop.
  • Chrome crashes with a message about shared memory. This is common in containers or low-memory environments. Add --no-sandbox (only if you trust the URLs) and --disable-dev-shm-usage.

Frequently Asked Questions

Do I need to install any additional tools?

No, just Chrome. The commands use built-in shell features.

Can I capture screenshots of pages that require login?

Headless Chrome can use your existing Chrome profile with --user-data-dir to include cookies, but this is advanced. For most cases, you'll need to handle authentication separately.

What format are the screenshots saved in?

PNG by default. You can use --screenshot=file.jpeg to save as JPEG, but PNG is recommended for quality.

How can I capture a specific element instead of the whole page?

Headless Chrome's built-in screenshot command doesn't support element capture. You'd need a tool like Puppeteer or Selenium for that.

Does this work with Chrome on all operating systems?

Yes, the headless mode is available on Windows, macOS, and Linux. The command-line syntax is the same, but the path to the Chrome executable varies.

Can I schedule this to run automatically?

Yes, you can use Task Scheduler on Windows or cron on macOS/Linux to run your script at regular intervals.