User Agents

Web browsers and everyday web tech, explained

Attach Remote Debugger to Headless Chrome in Selenium

· 5 min read · Browsers

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

Attaching a remote debugger to headless Chrome in Selenium WebDriver lets you inspect the browser's DOM, network activity, console logs, and even take screenshots while your tests run. This is invaluable for debugging flaky tests or understanding what the browser sees at a specific moment. You'll need Chrome/Chromium installed, the ChromeDriver binary that matches your Chrome version, and Selenium WebDriver set up in your preferred language (examples here use Python, but the concept applies to Java, C#, etc.).

What You'll Need

  • Chrome or Chromium browser (version 112 or later recommended for full headless debugging support)
  • ChromeDriver executable matching your Chrome version
  • Selenium WebDriver installed (e.g., pip install selenium for Python)
  • A debugging client: Chrome DevTools (open chrome://inspect), or a tool like Puppeteer's connect, or a simple WebSocket client

Step-by-Step

  1. Start Chrome with the remote debugging port. The key is to launch Chrome with the --remote-debugging-port flag. In Selenium, you do this by adding an argument to the ChromeOptions. For example, in Python:
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--remote-debugging-port=9222')
    driver = webdriver.Chrome(options=options)
    This starts headless Chrome with the DevTools protocol listening on port 9222.
  2. Verify the debugging endpoint is active. After starting the driver, open a browser (or use curl) to navigate to http://localhost:9222/json/version. You should see a JSON response with the browser's WebSocket URL and version info. If you get a connection refused error, the port is not open – check that Chrome actually started with the flag.
  3. Connect your debugger. Open Chrome and go to chrome://inspect. Under “Discover network targets,” click “Configure…” and add localhost:9222. You’ll see your headless browser listed. Click “inspect” to open DevTools attached to that browser. Alternatively, you can use the WebSocket URL from the JSON response to connect programmatically (e.g., with a WebSocket client in your test script).
  4. Use the debugger during test execution. With DevTools open, you can set breakpoints in your JavaScript, inspect elements, watch network requests, and evaluate expressions in the console. To make this practical, you might want to pause execution at a specific point. In Selenium, you can add a time.sleep(30) or a manual input prompt to give yourself time to inspect before the test continues.
  5. Close the debugger session. When done, close the DevTools window and quit the driver normally: driver.quit(). This ensures Chrome and ChromeDriver are properly terminated.

Common Mistakes

  • Using the wrong port. If you specify port 9222 but something else is already using it, Chrome will fail silently or pick another port. Check the json/version endpoint to confirm the actual port.
  • Forgetting to enable headless mode. If you don't add --headless, Chrome will open a visible window, which may not be what you want for CI. But note: the remote debugging works in both modes.
  • Not matching ChromeDriver version. ChromeDriver must match your Chrome browser version. Mismatched versions cause errors like “session not created.” Use the same major version.
  • Assuming DevTools opens automatically. The remote debugging port only exposes the protocol; you still need to manually open chrome://inspect or connect via a client. It doesn't pop up automatically.

Tips

  • Use a non-default port like 9223 if 9222 is commonly occupied on your machine.
  • For automated debugging, consider using the --remote-debugging-pipe flag instead of a port, which is more secure and avoids port conflicts, but requires a different connection method.
  • In CI environments, you can expose the debugger port via Docker port mapping to inspect tests manually.
  • Combine with Selenium's save_screenshot() to capture the state before you start debugging, so you have a reference.
  • If you need to debug a specific test, add a conditional breakpoint in your test code (e.g., if test_name == 'foo': time.sleep(60)).

Troubleshooting

  • Connection refused on port 9222. This means Chrome didn't start with the flag. Check that you passed the argument correctly in your options. Also ensure no firewall is blocking localhost connections.
  • chrome://inspect shows nothing. Click “Configure…” and ensure you've added localhost:9222. Also try restarting Chrome after adding the target.
  • DevTools opens but shows a blank page. This can happen if you're inspecting the browser but not a specific tab. Click on the target’s title to open the correct tab's DevTools.
  • Session not created error. Usually a ChromeDriver/Chrome version mismatch. Update ChromeDriver to match your Chrome version.
  • Debugger disconnects when test ends. When driver.quit() is called, the browser closes and the debugger loses connection. To keep it open, add a delay before quitting or don't quit until you're done.

Frequently Asked Questions

Can I attach the debugger to an already running Selenium test?

No, you must start Chrome with the remote debugging flag from the beginning. You cannot attach to an existing session that wasn't launched with the flag.

Does this work with Chrome for Testing or Chromium?

Yes, both support the same DevTools protocol. Just ensure you use the matching ChromeDriver.

Can I use this with Selenium Grid?

Yes, but you need to configure the node to expose the debugger port. Typically you'd map the port in Docker or use a custom node configuration.

Is there a way to debug without opening a separate browser window?

Yes, you can use a WebSocket client to connect to the DevTools protocol directly from your test code. Libraries like websocket-client in Python allow you to send commands and receive events programmatically.

Will this slow down my tests?

Only if you actively use the debugger. The overhead of just having the port open is minimal. However, if you add manual pauses, tests will take longer.

Can I take screenshots via the debugger?

Yes, the DevTools protocol has a Page.captureScreenshot command. You can send it via WebSocket to capture a screenshot without using Selenium's built-in method.