DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk4 min

How to Use a Chrome DevTools Protocol Session with Puppeteer

Create and manage Puppeteer Chrome DevTools Protocol sessions: attach to a page or target, send commands, handle events, detach safely, and troubleshoot protocol errors.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a page-level Chrome DevTools Protocol (CDP) session in Puppeteer, call await page.createCDPSession(). Use the returned session’s send() method for protocol commands and on() to listen for events; call detach() when finished. For a debuggable target that you are handling as a Puppeteer Target rather than a Page, create the session with target.createCDPSession().

Create and use a page-level CDP session

Puppeteer’s Page.createCDPSession() creates a CDP session attached to that page. A session is a raw interface to the browser’s DevTools protocol: send protocol method names and their parameters, and subscribe to protocol events. The following adapts Puppeteer’s documented Animation-domain example; confirm that the commands are supported by the browser and protocol version you use.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();
  await page.goto('https://example.com');

  const session = await page.createCDPSession();
  try {
    await session.send('Animation.enable');

    session.on('Animation.animationCreated', event => {
      console.log('Animation created', event);
    });

    const result = await session.send('Animation.getPlaybackRate');
    console.log('Playback rate:', result.playbackRate);

    await session.send('Animation.setPlaybackRate', { playbackRate: 2 });
  } finally {
    await session.detach();
  }
} finally {
  await browser.close();
}

For the installed release’s method signatures and protocol notes, see the Puppeteer Page.createCDPSession() reference and its CDPSession reference. The API pages reviewed identify Puppeteer 25.12.0 for several methods; the CDPSession details also appear in the Next reference, so check the documentation matching your installed Puppeteer version.

Send commands and handle results

Call session.send(method, parameters) and await its promise. The method name is a protocol method such as Animation.getPlaybackRate; the resolved value contains that command’s result fields. Commands that take no parameters can be sent with just the method name, as with Animation.enable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Listen for events

Register a listener with session.on(eventName, callback), using the protocol event name, such as Animation.animationCreated. Register listeners while the session is attached and before the event you need may occur. Event availability and payloads depend on the protocol supported by the browser.

Choose where to attach

Use a Page for page work

When your workflow already has a Puppeteer Page, page.createCDPSession() is the direct page-level method. Puppeteer marks Page.target() obsolete and points page-session users to Page.createCDPSession(); avoid using page.target().createCDPSession() as the preferred page recipe. See the Page.target() reference.

Use a Target for target-level work

A Puppeteer target is a debuggable entity; documented examples include a frame, page, or worker. If you already have the relevant Target and need a session attached to it, call await target.createCDPSession(). This is an attachment-point choice, not a general performance or reliability advantage over a page session. See Target.createCDPSession() and the Puppeteer API reference.

Detach and manage the session lifetime

Call await session.detach() once the commands and event listeners are no longer needed. After detachment the session cannot send messages and does not emit events. The nested try/finally structure in the example ensures cleanup even if a command fails, and closes the browser separately. Do not directly instantiate or subclass CDPSession; Puppeteer documents its constructor as internal.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Connect to an existing browser when needed

Creating a session from a page or target is separate from connecting Puppeteer to a browser. For an existing browser, Puppeteer’s ConnectOptions documents browserURL and browserWSEndpoint as connection settings. It also documents protocolTimeout for individual CDP calls, with a default of 180,000 milliseconds on the API page reviewed. That is a version-sensitive documented default, not a guarantee for every installed release; verify the options in the ConnectOptions reference for your version.

Troubleshoot common CDP session failures

  • Unsupported operation or method: CDP commands depend on the active protocol. If Puppeteer reports UnsupportedOperation, check whether the target browser’s protocol supports that operation and whether the method name and parameters match that protocol.
  • Commands fail after detaching: A detached session cannot send messages or emit events. Keep it attached for the full period in which commands and listeners are required; create a new session from the page or target if another session is needed.
  • Connection closed: Puppeteer documents ConnectionClosedError for a closed underlying connection. Check whether the browser or its connection ended before the command completed; this is distinct from a protocol method returning an error.
  • Protocol command error: Puppeteer documents ProtocolError for protocol errors. Inspect the failing method, its parameters, the target, and the browser’s protocol support rather than treating every failure as a transport problem.
  • Wrong attachment point: Use the page method for a page workflow, or the target method when you are operating on a target directly. Do not use the obsolete Page.target() route for the recommended page-level session.

For error and target details, consult the Puppeteer API reference; API behavior can vary with the installed Puppeteer and browser versions.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup:

If your goal is to retrieve a website screenshot rather than issue arbitrary CDP commands, ScreenshotNeo provides a one-request screenshot API. It is not a replacement for a raw CDP session. For a screenshot, call it with cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for parameters. Before capture, it accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers identify the page verdict and billing status. ScreenshotNeo also has an MCP server with take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000. Sign up for ScreenshotNeo’s free plan.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can a CDP session be used after calling detach()?

No. Detachment ends the session’s ability to send messages and emit events.

Should I construct a CDPSession directly?

No. Obtain one from a Puppeteer page or target; Puppeteer documents the constructor as internal.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.