Remote Debugging with Ruby Selenium & Headless Chrome
In this article
Remote debugging lets you attach a debugging client (like Chrome DevTools) to a running Selenium WebDriver session, even when the browser is headless. This is invaluable for inspecting page state, debugging complex interactions, or capturing network activity during automated tests. This guide walks you through enabling remote debugging for a Ruby Selenium script using headless Chrome.
What You'll Need
- Ruby installed (2.6+ recommended)
- Selenium-webdriver gem (
gem install selenium-webdriver) - Chrome browser installed (recent version)
- ChromeDriver matching your Chrome version (download from chromedriver.chromium.org or use
webdriversgem) - Basic familiarity with Ruby and Selenium
Step-by-Step
- Install necessary gems. In your Gemfile or via command line:
gem install selenium-webdriver. Optionally installwebdriversto auto-manage ChromeDriver:gem install webdrivers. - Require libraries in your Ruby script. Add
require 'selenium-webdriver'andrequire 'webdrivers'if you're using the webdrivers gem. - Configure Chrome options for headless and remote debugging. Create a
Selenium::WebDriver::Chrome::Optionsinstance. Add arguments:--headless=new(or--headlessfor older versions),--remote-debugging-port=9222, and optionally--remote-debugging-address=127.0.0.1for security. Example:options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless=new') options.add_argument('--remote-debugging-port=9222') options.add_argument('--remote-debugging-address=127.0.0.1') - Initialize the driver with those options. Use
Selenium::WebDriver.for :chrome, options: options. This starts Chrome in headless mode with the debugging port open. - Run your Selenium script as usual. Navigate to pages, interact with elements, etc. The debugging port remains active for the duration of the session.
- Connect a debugging client. Open Chrome (regular, not headless) and navigate to
http://127.0.0.1:9222. You'll see a list of open tabs. Click on any tab to open DevTools for that page. Alternatively, use the Chrome DevTools Protocol (CDP) directly via WebSocket. - Use DevTools features. You can now inspect elements, view console logs, monitor network requests, and even run JavaScript in the context of the page—all while the headless browser runs your tests.
- Close the driver when done. Call
driver.quitto terminate the browser and free the port.
Common Mistakes
- Using the wrong port. Ensure the port you specify is free. If another process uses 9222, pick a different one like 9223.
- Forgetting the headless argument. Without
--headless, Chrome opens a visible window, which may not be desired. - Not using the same Chrome version for ChromeDriver. Mismatched versions cause errors. Use the webdrivers gem or manually download the matching driver.
- Binding to all interfaces. If you omit
--remote-debugging-address=127.0.0.1, Chrome listens on all interfaces, which is a security risk. Always restrict to localhost.
Tips
- Use a unique port per test run if you run parallel tests to avoid conflicts.
- Combine remote debugging with Selenium's
page_sourceor screenshots for even deeper debugging. - You can also attach to an already-running Chrome instance by using the same debugging port—this is handy for debugging a test that fails intermittently.
- For automated debugging, consider using CDP via the
selenium-webdriver'sexecute_cdpmethod to evaluate JavaScript or capture performance metrics.
Troubleshooting
Port already in use: Change the port number in the options. Check with lsof -i :9222 (macOS/Linux) or netstat -ano | findstr :9222 (Windows).
Connection refused when accessing http://127.0.0.1:9222: Ensure the Chrome process is still running. If it exited (e.g., due to a script error), restart the driver.
ChromeDriver version mismatch: Update ChromeDriver to match your Chrome version. The webdrivers gem can do this automatically.
Headless mode not working with DevTools: Use --headless=new for Chrome 112+; older versions may need --headless only.
Cannot see tabs in the debugger: Sometimes the page list is empty if the page hasn't loaded. Navigate to a URL first, then refresh the debugger page.
Frequently Asked Questions
Can I use remote debugging with a non-headless Chrome?
Yes. Simply omit the --headless argument. The debugging port works the same way.
How do I attach to an already-running Chrome session?
Start Chrome manually with the same debugging port arguments (e.g., chrome --headless --remote-debugging-port=9222), then in your Ruby script use Selenium::WebDriver.for :chrome, options: options with the same port. Selenium will connect to the existing instance.
Is remote debugging secure?
It can be risky if exposed to the network. Always bind to 127.0.0.1 and avoid using it in production environments.
Can I use CDP commands directly from Ruby?
Yes. Selenium WebDriver's execute_cdp method allows you to send Chrome DevTools Protocol commands. For example, driver.execute_cdp('Network.enable').
Does remote debugging slow down my tests?
Minimally. The overhead is negligible for most test suites. However, if you keep DevTools open, it may consume extra memory.
What if I don't see the page list at http://127.0.0.1:9222?
Ensure you're using the correct port and that Chrome is still running. Also, try accessing http://localhost:9222 instead of 127.0.0.1 if you have proxy settings.