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 DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

How to Create a Puppeteer CDP Session

Attach a CDP session to a Puppeteer page with page.createCDPSession(), then use send() for commands and on() for events.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use await page.createCDPSession() to attach a Chrome DevTools Protocol (CDP) session to a Puppeteer page. Then call send() to issue protocol commands and on() to listen for protocol events. When finished, call detach() if you want to end the session explicitly.

Create a CDP session for a page

The direct page-level API is Page.createCDPSession(). The following example launches a browser, creates a page session, enables the Animation domain, listens for an animation event, and then detaches the session before closing the browser. It follows Puppeteer’s documented API and Animation-domain example; confirm that the command is available in the browser version your project uses.

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();

try {
  const page = await browser.newPage();
  const cdp = await page.createCDPSession();

  await cdp.send('Animation.enable');
  cdp.on('Animation.animationCreated', event => {
    console.log(event);
  });

  // Use the page and CDP session here.

  await cdp.detach();
} finally {
  await browser.close();
}

The session returned by this call is attached to the page. Puppeteer documents the method in its Page.createCDPSession() API reference.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Send commands and listen for events

A CDPSession is Puppeteer’s interface for raw Chrome DevTools Protocol communication. Call send(method, params) to send a protocol method, and subscribe to a protocol event with on(event, listener). For example, await cdp.send('Animation.enable') enables that domain; the listener receives the event payload when Animation.animationCreated fires.

Check the relevant protocol definition and browser version before relying on a command. The session API does not guarantee that every CDP command is available in every Chrome or Chromium release. Puppeteer’s CDPSession reference describes the session interface and includes an official Animation-domain example.

Choose the right attachment point

Need API When to use it
Attach to a page await page.createCDPSession() The direct, current page-level method.
Attach to another debuggable target await target.createCDPSession() Use when your chosen CDP target, rather than the page API, is the intended attachment point. Puppeteer describes targets as debuggable entities such as frames, pages, or workers.

See Puppeteer’s Target.createCDPSession() reference for target-level attachment. Do not use page.target().createCDPSession() as the recommended page route: Puppeteer marks Page.target() obsolete and directs users to Page.createCDPSession() instead (Page.target() reference).

Detach and understand session state

Calling await cdp.detach() detaches the session from its target. A detached session no longer emits events and cannot send messages. The read-only cdp.detached property reports whether it is detached. Do not construct or subclass CDPSession yourself; Puppeteer marks its constructor internal.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Check browser and protocol compatibility

Puppeteer’s documented protocol selection defaults depend on how the browser is used: launching Chrome selects CDP, launching Firefox selects WebDriver BiDi, and connecting to a browser selects CDP. These defaults are documented in the current ConnectOptions reference and may evolve, so verify the configuration in your own project when CDP support matters.

Puppeteer’s API reference links to the DevTools Protocol Viewer and the “Getting Started with DevTools Protocol” material for protocol details. Validate an individual command against the protocol and browser version you actually target.

Troubleshoot common problems

  • The session cannot send a command: Check whether detach() has already been called. Detached sessions cannot send messages; create a new session for the target if needed.
  • No event arrives: Confirm that the relevant protocol domain has been enabled, that the event name is correct, and that the browser command/event exists in the protocol version in use. Register the listener before the action that should trigger the event.
  • The method is unavailable: Use page.createCDPSession() for a page rather than the obsolete page.target() route. Confirm the Puppeteer version and API surface in your project.
  • CDP behavior differs by browser: Check whether your setup launches Chrome, launches Firefox, or connects to a browser, then verify the runtime protocol configuration. CDP-specific commands require a CDP-compatible connection.
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 a website screenshot rather than custom CDP control, ScreenshotNeo returns an image or PDF from one GET request. Cookie banners, popups, and chat widgets are removed before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots, and 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000. See the API documentation.

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

Sign up for 1,000 free screenshots a month, with no card.

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

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
Windows Errors? Fix Them Before They SpreadFree repair scan
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.