---
title: "What is Chrome DevTools Protocol (CDP)?"
description: "Learn how Chrome DevTools Protocol (CDP) works, when to use it, and how to control Chrome with a step-by-step WebSocket and Puppeteer example."
canonical: "https://browser-use.com/posts/what-is-chrome-devtools-protocol"
last_updated: "2026-09-25"
---

# What is Chrome DevTools Protocol (CDP)?

**Author:** Nik Tomazic
**Date:** 2026-09-25
> Learn how Chrome DevTools Protocol (CDP) works, when to use it, and how to control Chrome with a step-by-step WebSocket and Puppeteer example.

---

Chrome DevTools Protocol (CDP) is a low-level [protocol for inspecting and controlling Chromium-based browsers](https://chromedevtools.github.io/devtools-protocol/). It exposes browser capabilities such as JavaScript execution, DOM inspection, network monitoring, and page rendering through structured messages.

Chrome DevTools, the developer interface built into Chrome, uses this protocol to communicate with the browser. Automation libraries such as Playwright and Puppeteer also use CDP to control Chromium. CDP works in both headful and [headless](https://browser-use.com/posts/what-is-a-headless-browser) mode.

In this article, we'll explain how CDP works, what it can do, and how to use it through a practical example.

## How Does CDP Work?

CDP uses bidirectional communication and follows a client-server model. The client sends commands, and the browser returns responses. The browser also sends events when something happens, such as when a request starts or a page finishes its initial load.

![A CDP client sends commands to Chrome, which returns matching responses and independent events](https://browser-use.com/images/what-is-chrome-devtools-protocol/cdp-message-flow-6aacd0320a.webp)

Communication typically happens over a WebSocket connection, but OS-level pipes are also supported for local communication.

CDP's capabilities are grouped into domains. For example, `Page` handles navigation, `Runtime` evaluates JavaScript, `Network` reports requests and responses, and `Tracing` records performance data. 

### JSON-RPC-Based Messages

CDP uses an adapted form of [JSON-RPC](https://www.jsonrpc.org/specification), a remote procedure call protocol:

- A command has an `id`, a `method`, and optional `params`.
- Its response repeats the `id` and contains `result` or `error`.
- An event has a `method` and potentially `params`, without a request `id`.

This lets the client match responses to commands while receiving events independently. 

CDP messages normally omit the `jsonrpc` version field required by JSON-RPC 2.0, so describing CDP as fully compliant with JSON-RPC 2.0 is inaccurate.

### Targets and Sessions

Targets and sessions are two of the most important concepts in CDP.

A target is something you can debug or control. That can be a page, tab, iframe, worker, and so on. Each target generally has a `targetId`.

A session, on the other hand, is the connection you use to communicate with a specific target. When you attach to a target, Chrome gives you a `sessionId`, which tells Chrome which target your commands are meant for.

Here's an example to better understand how they're connected:

```
Chrome Browser
│
├── Target: Tab A
│   └── Session A
│       ├── Runtime.evaluate
│       ├── Network.enable
│       └── Page.navigate
│
├── Target: Tab B
│   └── Session B
│
└── Target: Service Worker
    └── Session C
```

## What Can You Use CDP For?

CDP supports tasks ranging from diagnostics to [browser automation](https://browser-use.com/posts/browser-automation-tools).

### Testing and Debugging

You can inspect network traffic, capture console output, emulate device settings, and collect performance traces. These capabilities can help you understand why a page behaves differently under particular conditions.

### Scraping and Document Generation

CDP can also be used for scraping and document generation. JavaScript evaluation and DOM inspection expose rendered content, while screenshot and PDF commands can capture the page's visual output.

Note that for this use case, you're usually better off using an automation library. More about this in a later section.

> Want to learn web scraping? Check out [The Ultimate Guide to Web Scraping](https://browser-use.com/posts/web-scraping-guide-2026).

### Browser Agents

The protocol can also be used for giving [AI-powered browser agents](https://browser-use.com/posts/browser-agents) control over your browser. An agent's tools can translate actions into CDP commands and return page information to the model. For an engineering example, see [why Browser Use moved from Playwright to direct CDP](https://browser-use.com/posts/playwright-to-cdp).

## CDP Limitations and Common Pitfalls

Moving along, let's look at some common pitfalls.

### Browser and Version Compatibility

CDP is mainly designed for Chromium-based browsers. If you need standardized automation across different browsers, you'll have to use WebDriver or [WebDriver BiDi](https://www.selenium.dev/documentation/webdriver/bidi/).

Playwright supports multiple browsers, but that doesn't mean it uses CDP for all of them. Its [`connectOverCDP`](https://playwright.dev/docs/api/class-browsertype#browser-type-connect-over-cdp) method only works with Chromium and provides less functionality than Playwright's normal connection.

CDP support can also vary between browser versions. New or experimental commands may not exist in older versions of Chrome. You can check the protocol supported by a running browser at `/json/protocol`.

### Complex State Management

Pages and targets can change while your program is running. For example, navigating to a new page can destroy existing JavaScript execution contexts and invalidate old remote object IDs.

New tabs, frames, and workers can also create new targets that you may need to attach to and clean up later.

A script that works for one simple page doesn't automatically handle these changes.


| Symptom | What to check |
| --- | --- |
| A page command fails on the browser connection | Attach to the page target and use its session ID. |
| A load event never arrives | Enable Page events and start listening before navigating. Also use a timeout. |
| A method isn't found | Check the CDP domain, target type, and the protocol version supported by your browser. |


## When Should You Use CDP Directly?

For most automation, start with a library and add lower-level control where needed.

### Playwright or Puppeteer

Use these for tests, scraping, forms, and screenshots. They provide page and locator APIs; Playwright also [waits for elements to be actionable](https://playwright.dev/docs/actionability). You write the workflow instead of managing protocol messages.

To learn how these tools compare with Selenium, see [Playwright vs Selenium vs Puppeteer](https://browser-use.com/posts/playwright-vs-selenium-vs-puppeteer).

### CDP Sessions

Use a library's [`CDPSession`](https://pptr.dev/api/puppeteer.cdpsession) when you need a Chromium command or event its normal API doesn't expose. You keep the library's navigation and selectors while sending selected protocol commands.

### Direct WebSocket Commands

Use these for learning CDP or building protocol tooling. You manage response IDs, timeouts, errors, and target lifetimes. Chrome's [Protocol Monitor](https://developer.chrome.com/docs/devtools/protocol-monitor) helps you inspect messages before implementing them.

## Practical Example: Navigate and Read a Page Title

Let's put CDP into practice. We'll connect to headless Chrome, navigate to a page, and read its title from Node's interactive console.

> This walkthrough was tested on macOS with Chrome 153.0.8010.53 and Node.js 22.22.3.

### Start Chrome (with CDP enabled)

First, start Chrome with remote debugging enabled. Open a terminal and run:

```bash
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" \
  --headless \
  --remote-debugging-port=9222 \
  --user-data-dir="/tmp/cdp-article-profile" \
  about:blank
```

Here, `--remote-debugging-port` enables debugging on port `9222`. We use `--user-data-dir` for a separate profile: [since Chrome 136](https://developer.chrome.com/blog/remote-debugging-port), regular Chrome ignores remote-debugging switches when they target its default data directory.

If that port is taken, use another throughout the example. Keep it off the public internet: anyone who connects could control Chrome.

Leave Chrome running while we connect from another terminal.

### Connect to the Page

Next, open another terminal and start Node's interactive console:

```bash
node
```

Enter these statements individually, waiting for the `>` prompt after each:

```javascript
const response = await fetch("http://127.0.0.1:9222/json/list");
const targets = await response.json();
const page = targets.find(target => target.type === "page");
page.webSocketDebuggerUrl
```

Here, we fetch Chrome's targets and select a page. You should see its WebSocket address, something like `ws://127.0.0.1:9222/devtools/page/...`. We'll send our commands there.

Next, connect to the WebSocket:

```javascript
const ws = await new Promise((resolve, reject) => {
  const socket = new WebSocket(page.webSocketDebuggerUrl);
  socket.onopen = () => resolve(socket);
  socket.onerror = reject;
});
```

Once the prompt returns, add a handler to print Chrome's responses and events:

```javascript
ws.onmessage = ({ data }) => console.log(data);
```

We're now connected directly to the page, so we don't need a `sessionId`.

For a browser-level connection, use the `webSocketDebuggerUrl` from `http://127.0.0.1:9222/json/version` instead. Its path contains `/devtools/browser/`. From that connection, attach to a page with `Target.attachToTarget` and `flatten: true`, then include the returned `sessionId` in commands for that page. The page connection above keeps this walkthrough simpler.

### Enable Events and Navigate

Before navigating, enable Page events so Chrome can tell us when the page loads:

```javascript
ws.send(JSON.stringify({ id: 1, method: "Page.enable" }));
```

You should receive the following response before continuing:

```json
{ "id": 1, "result": {} }
```

Notice the matching `id`: this response belongs to our command. An empty `result` means it succeeded without returning additional data.

Next, let's tell Chrome to navigate to Example Domain:

```javascript
ws.send(
  JSON.stringify({
    id: 2,
    method: "Page.navigate",
    params: { url: "https://example.com" },
  })
);
```

You should see a response and page events. Look for these messages; IDs are shortened and unrelated events omitted:

```json
{
  "id": 2,
  "result": {
    "frameId": "A7789DDD...",
    "loaderId": "C6E15414...",
    "isDownload": false
  }
}
```

```json
{
  "method": "Page.loadEventFired",
  "params": {
    "timestamp": 181921.758042
  }
}
```

The command's response doesn't mean loading has finished. Wait for `Page.loadEventFired` before moving on. Our handler catches this event because we installed it before navigating.

If navigation fails, check `error` or `result.errorText`. For dynamic apps, also wait for the content you need; document load doesn't guarantee it's ready.

### Read the Title and Disconnect

With the document loaded, let's read its title using [`Runtime.evaluate`](https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate). This CDP command executes a JavaScript expression in the inspected page's context and returns a description of the result. Here, the expression is `document.title`:

```javascript
ws.send(
  JSON.stringify({
    id: 3,
    method: "Runtime.evaluate",
    params: {
      expression: "document.title",
      returnByValue: true,
    },
  })
);
```

You should get a response like this:

```json
{
  "id": 3,
  "result": {
    "result": {
      "type": "string",
      "value": "Example Domain"
    }
  }
}
```

You can find `Example Domain` at `result.result.value`. The outer `result` holds the response; the inner one describes the JavaScript value. Check `result.exceptionDetails` if evaluation fails.

Two parameters matter when you extend this example:

- `returnByValue: true` returns JSON-serializable objects as values. Without it, an object result normally comes back as a remote reference with an `objectId`. Primitive values such as this title string are returned directly either way.
- `awaitPromise: true` waits for a promise returned by the expression to resolve. It does not automatically wait for the page's other requests or UI updates.

Evaluation runs in the page's JavaScript environment, so browser access rules still apply. To evaluate in another execution context within the attached target, such as an isolated world, set `contextId`.

Finally, let's close the connection:

```javascript
ws.close();
```

Then exit Node:

```text
.exit
```

### The Same Task With Puppeteer

Let's look at how we could perform the same task using Puppeteer.

First, install it:

```bash
npm install --save-exact puppeteer-core@25.11.0
```

Next, create `puppeteer-demo.mjs` and add the following code:

```javascript
import puppeteer from "puppeteer-core";

// Connect to the Chrome instance we already started.
const browser = await puppeteer.connect({
  browserURL: "http://127.0.0.1:9222",
});
try {
  // Reuse its open page and wait for document load.
  const [page] = await browser.pages();
  await page.goto("https://example.com", { waitUntil: "load" });
  console.log("Title:", await page.title());
} finally {
  // Disconnect, leaving Chrome running.
  await browser.disconnect();
}
```

Execute the script:

```bash
node puppeteer-demo.mjs
```

You should see `Title: Example Domain`. This time, `page.goto()` navigates and waits for document load, while `page.title()` retrieves the title.

As you can see, doing the same thing with Puppeteer is way easier. It doesn't require sending raw commands, reading CDP responses, juggling sessions, and so on.

When you're finished, stop Chrome with Ctrl+C in the terminal where you started it.

## Conclusion

CDP gives software direct access to Chromium's browser capabilities, making it useful for debugging, scraping, and AI-powered browser agents. Its commands, responses, and events let programs inspect what's happening and act on it.

The tricky part is managing a browser that keeps changing. Pages navigate, targets appear and disappear, and commands vary between versions. Understanding how domains, targets, and sessions fit together helps you handle those changes and troubleshoot automation, even when a library handles the protocol for you.

> If you need a remote browser for your existing automation, [Browser Use Browser Infrastructure](https://browser-use.com/stealth-browsers) provides CDP-compatible browsers you can [control with Playwright or Puppeteer](https://docs.browser-use.com/cloud/browser/playwright-puppeteer-selenium).

---

BROWSER USE

- Browser Use Agents: give Browser Use a task and receive completed work. API V4 is current for new integrations.
- Browser Infrastructure: connect your agent or automation to managed browsers through SDK, REST, or CDP. Starts at $0.02/browser-hour.
- Developer tools: Open Source, Browser Harness, SDK, and MCP support the two products above.

[Developer Index](https://browser-use.com/index.md)
[Product Map](https://browser-use.com/llms.txt)
[Full Product Context](https://browser-use.com/llms-full.txt)
[Pricing](https://browser-use.com/pricing.md)
[Cloud Docs](https://docs.browser-use.com/cloud/quickstart)
[Open Source Docs](https://docs.browser-use.com/open-source/introduction)
