Chrome DevTools Protocol Logs: How to Capture Sessions
In this article
Capturing Chrome DevTools Protocol (CDP) logs allows developers and QA engineers to record the low-level communication between the browser engine and the DevTools frontend. Unlike standard network logs or console outputs, CDP logs provide a granular view of every command sent to the browser and every event emitted by the browser, making it indispensable for debugging automation scripts, analyzing performance bottlenecks, or troubleshooting complex rendering issues that aren't visible in the standard Inspector.
What You'll Need
- A Chromium-based browser (Google Chrome, Microsoft Edge, or Brave).
- Administrative access to the machine to launch the browser via the command line.
- A text editor or log viewer capable of handling large JSON or text files (e.g., VS Code, Sublime Text, or Notepad++).
- Basic familiarity with the terminal or command prompt.
Step-by-Step
- Close All Browser Instances: To ensure the remote debugging port opens correctly, you must completely shut down all running instances of Chrome. Check your system tray or Activity Monitor/Task Manager to ensure no background processes are still active. If the browser is already running, the new command-line flags may be ignored.
-
Launch Chrome with Remote Debugging Enabled: Open your terminal (macOS/Linux) or Command Prompt/PowerShell (Windows). You need to launch the browser with a specific flag that tells Chrome to listen for CDP connections.
For Windows:"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
For macOS:/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
The port 9222 is the industry standard, but you can choose any open port. This flag enables the WebSocket server that allows external tools to communicate with the browser. -
Enable Logging via Command Line Flags: While the remote debugging port allows interaction, it doesn't automatically save a log file to your disk. To capture the internal protocol traffic, add the following flags to your launch command:
--log-net-log=cdp_logs.json --net-log-capture-mode=IncludeCDP
The--log-net-logflag specifies the filename and path where the log will be saved, and--net-log-capture-mode=IncludeCDPexplicitly instructs the browser to include the DevTools Protocol messages in that log file. If you omit the latter, you will only get standard network events without the CDP command-response pairs. -
Perform the Browser Session: With the browser now running in the specialized mode, navigate to the website and perform the actions you wish to capture. Whether you are manually clicking elements or running a Puppeteer/Playwright script, every CDP command (such as
Page.navigateorRuntime.evaluate) and every event (such asPage.loadEventFired) is now being streamed to the log file. - Gracefully Shut Down the Browser: To ensure the log file is flushed from memory and properly closed on the disk, exit the browser through the menu (Three dots > Exit) rather than killing the process via the Task Manager. This prevents file corruption and ensures the final packets of the session are recorded.
-
Analyze the Logs: The resulting file is often a complex JSON structure. To make sense of it, you can use the Chrome NetLog viewer. Navigate to
chrome://net-export/in a separate browser window, click "Choose Log File," and upload yourcdp_logs.json. This provides a searchable, filtered interface to inspect the exact sequence of protocol messages.
Common Mistakes
One of the most frequent errors is attempting to launch the browser with the flags while a standard session is already open. Chrome often attaches new windows to existing processes; if a process is already running without the --remote-debugging-port flag, the new window will simply open in that existing process, and no logs will be captured. Always verify that the process list is clear before starting.
Another common pitfall is confusing standard "Network" tab logs with CDP logs. The Network tab shows HTTP requests and responses (the data moving between the client and server). CDP logs show the instructions moving between the DevTools interface and the browser engine (the data moving between the controller and the browser). If you are looking for API calls, use the Network tab; if you are looking for why a DOM.setAttributes call failed, you need CDP logs.
Users also frequently forget to specify the --net-log-capture-mode=IncludeCDP flag. Without this, the --log-net-log flag defaults to capturing only network-level events. You will find a large file, but when you open it in the NetLog viewer, the CDP section will be empty, leading to the mistaken belief that the protocol isn't working.
Tips
To keep your log files manageable, use a clean user profile for your capture sessions. You can do this by adding the --user-data-dir="C:\temp\chrome-profile" flag. This prevents your personal extensions and cached data from cluttering the logs with noise, as extensions often send their own CDP commands in the background.
If you are debugging a script that runs very quickly, consider using a debugger or adding a small delay at the start of your script. This gives you time to verify that the browser has launched correctly and that the log file has been initialized on the disk before the primary actions occur.
For those working in Linux environments or CI/CD pipelines, using the --headless flag in combination with CDP logging is highly effective. It allows you to capture protocol logs from a server environment where no GUI is available, providing a window into how the browser is behaving in a headless state.
Troubleshooting
Log file is empty or not created: First, check the permissions of the directory where you are saving the log. If you specify a path like C:\cdp_logs.json, the browser may fail to write the file due to administrative restrictions. Try saving it to a user folder like Documents or Desktop.
Port 9222 is already in use: If you receive an error stating the port is occupied, another application (or a zombie Chrome process) is using it. You can either find and kill the process using netstat -ano | findstr :9222 (on Windows) or simply change the flag to a different number, such as --remote-debugging-port=9223.
NetLog viewer fails to load the file: This usually happens if the browser crashed or was forced-closed, leaving the JSON file malformed. Ensure you close the browser normally. If the file is too large for the browser-based viewer to handle (several gigabytes), use a command-line JSON processor like jq to filter for specific CDP events before attempting to view it in the GUI.
CDP events are missing from the log: Double-check that you are not using a version of Chrome that is too old. CDP is evolving rapidly, and some capture modes were introduced in later versions. Ensure your browser is updated to the latest stable release.
Frequently Asked Questions
Does capturing CDP logs slow down the browser?
Yes, there is a slight performance overhead because the browser must serialize and write every protocol message to the disk. For most debugging tasks, this is negligible, but it can affect high-precision performance benchmarking.
Can I capture CDP logs from a mobile device?
You cannot run the command-line flags directly on a mobile device. However, you can connect a mobile device to a desktop via USB and use chrome://inspect/#devices. While this allows interaction, capturing a full local log file of the mobile session requires a proxy or a specialized automation framework like Appium.
Is there a way to filter CDP logs in real-time?
The --log-net-log method is for post-session analysis. For real-time filtering, you should connect a WebSocket client to the --remote-debugging-port and filter the incoming JSON stream using a script or a tool like Chrome DevTools itself.
What is the difference between CDP and the WebDriver protocol?
WebDriver is a standardized W3C protocol designed for cross-browser compatibility. CDP is a Chrome-specific protocol that provides much deeper access to the browser's internals, such as the memory heap, network emulation, and the rendering pipeline.
Do I need to be an administrator to use these flags?
You do not need system-wide administrator privileges to launch Chrome with these flags, but you do need write permissions for the folder where the log file is being saved.