How to Screenshot a Webpage That Blocks Headless Chrome
In this article
This guide shows you how to capture a full-page screenshot of a website that detects and blocks headless Chrome browsers. Instead of relying on a plain headless browser, you'll use a real Chrome instance with remote debugging, which bypasses most basic bot detection. You'll need Chrome installed, a text editor, and basic comfort with the command line.
What You'll Need
- Google Chrome (or Chromium) installed on your computer
- A terminal or command prompt
- A text editor (like Notepad or VS Code)
- Basic knowledge of navigating folders in the terminal
Step-by-Step
- Close all Chrome windows. Make sure no Chrome processes are running. You can check your system's task manager to confirm.
- Launch Chrome with remote debugging enabled. Open your terminal and run the following command, replacing
/path/to/chromewith the actual path to your Chrome executable. On Windows, it's usuallyC:\Program Files\Google\Chrome\Application\chrome.exe; on macOS, it's/Applications/Google Chrome.app/Contents/MacOS/Google Chrome; on Linux, it's oftengoogle-chromeorchromium.
This starts Chrome in a normal (non-headless) mode with a separate profile, listening on port 9222. The[chrome-path] --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-debug--user-data-diravoids using your personal profile, which can trigger detection. - Navigate to the target webpage. In the Chrome window that opened, go to the website you want to screenshot. Log in if needed, and let the page fully load. This step is crucial because the site sees a real browser, not a headless one.
- Open the DevTools protocol endpoint. In a new terminal tab, run
curl http://localhost:9222/json. You'll see a JSON list of open tabs. Find the entry for your page and copy itswebSocketDebuggerUrl(something likews://localhost:9222/devtools/page/...). - Send a screenshot command via WebSocket. You can use a simple Node.js script or a tool like websocat . Here's a minimal Node.js script that connects to the WebSocket and captures a full-page screenshot: const WebSocket = require('ws'); const fs = require('fs'); const ws = new WebSocket('ws://localhost:9222/devtools/page/YOUR_TAB_ID'); ws.on('open', () => { }); ws.on('message', (data) => { const msg = JSON.parse(data); if (msg.id === 1) { fs.writeFileSync('screenshot.png', Buffer.from(msg.result.data, 'base64')); console.log('Screenshot saved as screenshot.png'); ws.close(); process.exit(0); } }); Replace YOUR_TAB_ID with the ID from the WebSocket URL. Run the script with node script.js (you'll need to install the ws package first via npm install ws ).
- Check the output. The screenshot will be saved in the same folder as your script. Open it to verify it captured the full page, including content below the fold.
- Close Chrome and clean up. After capturing, close the Chrome window and the terminal processes. You can delete the temporary profile folder (
/tmp/chrome-debug) if you want.
Common Mistakes
- Using the default Chrome profile. If you don't specify a
--user-data-dir, Chrome may reuse your existing profile, which can cause the site to behave differently or trigger security warnings. - Not waiting for the page to fully load. If you capture too early, you'll get a blank or partially loaded page. Wait for network requests to finish and for any lazy-loaded images to appear.
- Capturing only the viewport. The default
Page.captureScreenshotcaptures only the visible area. You must setcaptureBeyondViewport: trueto get the full page height. - Forgetting to close other Chrome instances. If Chrome is already running, the remote debugging port may not open correctly. Close all instances first.
Tips
- Use a separate user-data-dir. This isolates your session and reduces the chance of detection based on cookies or extensions.
- Set a realistic viewport size. Some sites serve different layouts based on screen size. You can set the window size with
--window-size=1920,1080before launching. - Disable automation flags. Add
--disable-blink-features=AutomationControlledto your launch command to hide thenavigator.webdriverproperty that some scripts check. - Use a headless shell with flags. If you absolutely need headless mode, try
--headless=new(Chrome 112+) along with the same debugging flags. It's less detectable than the old headless mode. - Consider using Puppeteer with
headless: false. Puppeteer can drive a real Chrome window and take full-page screenshots easily, but it still uses automation flags unless you disable them.
Troubleshooting
- Port 9222 is already in use. Change the port number in the launch command and update the WebSocket URL accordingly.
- No tabs appear in the JSON list. Make sure Chrome is running with the debugging port open. Check that you used the correct path and that no other Chrome instance is running.
- WebSocket connection fails. Verify the URL is correct and that the tab ID hasn't changed. If you closed the tab, you'll need to reopen it.
- Screenshot is blank or black. The page may have rendered off-screen. Try scrolling to the top before capturing, or use
Page.captureScreenshotafter callingPage.enableand waiting a moment. - The site still blocks you. Some sites use advanced fingerprinting that detects real Chrome too. If that happens, you may need to use a different browser or a headless browser with stealth plugins, but that's beyond this guide's scope.
Frequently Asked Questions
Why does headless Chrome get blocked?
Headless Chrome often has a different user agent and exposes the navigator.webdriver flag, which many sites check to detect automated access. Using a real Chrome window avoids these signals.
Can I use this method to bypass paywalls or login walls?
No. This guide is for capturing pages you have legitimate access to. You should not use it to circumvent authentication or content restrictions.
Is there a simpler way using browser extensions?
Yes, extensions like GoFullPage or FireShot can capture full-page screenshots in a regular Chrome window. They work well for most sites, but if a site blocks extensions or uses heavy anti-bot measures, the DevTools method may be more reliable.
Will this work on any website?
It works on most sites that only block headless browsers. Sites with sophisticated bot detection (using IP reputation, behavioral analysis, etc.) may still block you, but that's rare.
Do I need to know programming to use this?
Basic familiarity with the terminal and running a script is enough. The Node.js script provided is minimal; you can also use a tool like websocat if you prefer not to code.
Can I automate this for multiple pages?
Yes, you can loop through URLs in your script, navigating to each page and capturing screenshots. Just make sure to wait for each page to load before capturing.