Implementation:Microsoft Playwright Client CDPSession
| Knowledge Sources | |
|---|---|
| Domains | Browser Automation, Chrome DevTools Protocol |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for interacting with browser targets via the Chrome DevTools Protocol (CDP) provided by the Playwright library.
Description
The CDPSession class extends ChannelOwner and implements the api.CDPSession interface. It provides a client-side wrapper for sending CDP commands and receiving CDP events from Chromium-based browsers. The constructor wires up the channel's 'event' emissions to re-emit them on the CDPSession instance, and explicitly binds event listener methods (on, addListener, off, removeListener, once) from the parent class. The send() method accepts a typed CDP method name and parameters, dispatches the command through the channel, and returns the typed result. The detach() method disconnects the session from the target.
Usage
Use CDPSession when you need low-level access to the Chrome DevTools Protocol for Chromium browsers, such as intercepting network traffic at the protocol level, accessing performance metrics, or controlling features not exposed through Playwright's high-level API. Obtain a session via page.context().newCDPSession(page) or browser.newBrowserCDPSession().
Code Reference
Source Location
- Repository: Microsoft_Playwright
- File:
packages/playwright-core/src/client/cdpSession.ts
Signature
export class CDPSession extends ChannelOwner<channels.CDPSessionChannel> implements api.CDPSession {
static from(cdpSession: channels.CDPSessionChannel): CDPSession;
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.CDPSessionInitializer);
async send<T extends keyof Protocol.CommandParameters>(
method: T,
params?: Protocol.CommandParameters[T]
): Promise<Protocol.CommandReturnValues[T]>;
async detach(): Promise<void>;
}
Import
import { CDPSession } from 'playwright-core/src/client/cdpSession';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| method | keyof Protocol.CommandParameters |
Yes | The CDP method name to invoke (e.g., 'Network.enable') |
| params | Protocol.CommandParameters[T] |
No | The parameters for the CDP command |
Outputs
| Name | Type | Description |
|---|---|---|
| send() | Promise<Protocol.CommandReturnValues[T]> |
The typed result of the CDP command |
| detach() | Promise<void> |
Detaches the CDP session from its target |
Usage Examples
const client = await page.context().newCDPSession(page);
// Enable network domain
await client.send('Network.enable');
// Listen for CDP events
client.on('Network.requestWillBeSent', (params) => {
console.log('Request:', params.request.url);
});
// Send a CDP command with parameters
const result = await client.send('Runtime.evaluate', {
expression: 'document.title',
});
console.log('Title:', result.result.value);
// Detach session
await client.detach();