How to Screenshot Hundreds of Web Pages with Headless Chrome
In this article
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
- 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.txtin a folder you can easily find. Make sure URLs include the full protocol (e.g.,https://example.com/page). - Open your terminal. On Windows, press
Win + R, typecmd, and press Enter. On macOS, open Spotlight (Cmd + Space) and typeTerminal. On Linux, use your system's terminal app. - Navigate to your folder. Use the
cdcommand to move into the directory where you savedurls.txt. For example:cd C:\Users\YourName\screenshots(Windows) orcd /Users/YourName/screenshots(macOS/Linux). - 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 usechrome.exeor the full path to the Chrome executable. If you get an error, see Troubleshooting. - 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). - 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 - 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 /nobreakinside the loop. On macOS/Linux, addsleep 3. - 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.
- 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://orhttps://; otherwise Chrome treats them as local file paths. - Using the wrong Chrome executable name. On Windows, the command might be
chrome.exeor the full path likeC:\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-sizeto control dimensions. Set width and height in pixels. For full-page screenshots, you may need a larger height or use the--full-pageflag (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
outputdirectory and modify the screenshot path tooutput/shot1.pngto keep things organized. - Log errors. Redirect error messages to a file:
2> errors.logon Windows, or2>> errors.logon 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-budgetor 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-dirflag 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).