Screenshot URLs in a Text File with Chrome Headless
In this article
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
- 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. - Open your command-line interface. On Windows, press
Win + R, typecmd, and press Enter. On macOS, open Terminal from Applications > Utilities. On Linux, use your preferred terminal emulator. - Navigate to the folder containing your text file. Use the
cdcommand to change directories. For example:cd C:\Users\YourName\Desktop(Windows) orcd ~/Desktop(macOS/Linux). - Test Chrome headless on a single URL. Run the following command to verify Chrome works in headless mode. Replace
https://example.comwith a real URL andtest.pngwith 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). - 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.
- 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 as1.png,2.png, etc. - For macOS/Linux (Bash): Use a
whileloop. Save the following asscreenshot.shand make it executable withchmod +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. - 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.
- 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.exeand located inC:\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 includehttps://orhttp://. - Blank screenshots due to missing
--virtual-time-budget. Some pages load content dynamically. Add--virtual-time-budget=5000to 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,HEIGHTflag controls the viewport. Use--window-size=1920,1080for full HD captures, but remember that some pages may look different at larger sizes. - Add a delay for slow pages. Use
--virtual-time-budget=10000to wait up to 10 seconds. This is more reliable than a fixed sleep. - Capture full-page screenshots. Add
--screenshot --full-page(note: the--full-pageflag 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!.pngto keep things organized.
Troubleshooting
- Chrome opens a normal window instead of headless. Ensure you use
--headless(or--headless=newfor newer Chrome versions). Also, the--disable-gpuflag 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-rasterizeror use--headless=new. - Command not found or permission denied. On Linux/macOS, you may need to use
google-chromeorchromium-browserinstead ofchrome. Also ensure the script is executable withchmod +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 /floop can be tricky with delayed expansion. Ensure you havesetlocal enabledelayedexpansionand 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.