Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk5 min

How to Use CDPSession in Puppeteer

Use Puppeteer’s page.createCDPSession() to send Chrome DevTools Protocol commands and listen for events, with guidance on scope, timeouts, and cleanup.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use await page.createCDPSession() to attach a Puppeteer CDPSession to a page, then call send() for Chrome DevTools Protocol commands and on() to handle protocol events. When you are finished, call detach(). This guide follows Puppeteer’s API Reference version 25.12.0 and uses the current page-scoped creation method.

What CDPSession does

Puppeteer describes CDPSession instances as being used “to talk raw Chrome Devtools Protocol.” The session gives your code access to CDP commands and events for its attached page; it is not a browser page or a replacement for Puppeteer’s higher-level page APIs. Its constructor is internal, so obtain sessions through Puppeteer’s creation methods rather than constructing or subclassing the class yourself. Puppeteer CDPSession class reference

As an Amazon Associate I earn from qualifying purchases.

Create a session for a page

For page-level work, call createCDPSession() on the Puppeteer Page object. It returns a promise that resolves to a session attached to that page. Page.createCDPSession() reference

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const client = await page.createCDPSession();

For example, assuming page is already an open Puppeteer page, the following sequence follows the class reference’s Animation example and adds cleanup at the end:

const client = await page.createCDPSession();

await client.send('Animation.enable');
client.on('Animation.animationCreated', () => {
  console.log('Animation created!');
});

const response = await client.send('Animation.getPlaybackRate');
await client.send('Animation.setPlaybackRate', {
  playbackRate: response.playbackRate / 2,
});

await client.detach();

The Animation domain example enables the domain, registers an event handler, reads the current playback rate, and sets it to half that rate. The example illustrates the API sequence; the documentation does not claim this exact combined snippet was tested as a unit. Protocol domains may have their own setup needs, so do not assume every command requires the same enable step. CDPSession class reference

Send a CDP command and receive its result

Call client.send(method, params) to invoke a protocol command. It returns a promise for the command’s mapped result, so use await when subsequent code depends on that result. For example, the Animation calls above await both the playback-rate query and the command that changes it. CDPSession.send() reference

Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

The method is typed around Puppeteer’s protocol command mapping: the command string determines the corresponding parameter and result types. Both params and options are optional in the method signature. The method reference does not give additional explanatory descriptions for those parameters, so check the relevant protocol command’s requirements when deciding what to pass.

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

Listen for CDP events

Register a handler with client.on(event, handler). In the example, the handler listens for Animation.animationCreated after the Animation domain is enabled. Choose the event name and any required domain setup for the CDP feature you are using; the example is specific to Animation rather than a universal setup recipe. The event subscription API is documented on the CDPSession class page.

Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Choose the right session scope

Creation method Attachment scope When to use it
page.createCDPSession() The page Use when the protocol work is for a Puppeteer page. This is the direct documented route for a page-scoped session. Reference
target.createCDPSession() A target Use when you specifically need a session attached to a target. Puppeteer defines targets as debuggable entities such as frames, pages, or workers; the Target method reference documents target-scoped creation. Its API page identifies version 25.10.0, so do not assume that page was synchronized with every 25.12.0 reference. Target.createCDPSession() reference · API Reference

Do not use page.target().createCDPSession() as the preferred page route. In the version 25.12.0 documentation, Page.target() is marked obsolete and points readers to Page.createCDPSession() for creating a session. The same page points to the PageEvent.Popup event for identifying pages spawned by the current page. Page.target() reference

Detach when the session is no longer needed

Call await client.detach() at the end of the session’s useful lifetime. Once detached, the session emits no more events and cannot send further messages. Put cleanup at the point where the code has finished using that session; do not try to reuse it afterward. CDPSession.detach() reference

Timeouts and troubleshooting

  • Session creation or a command fails: Confirm the page is available and that the command is valid for the attached target. CDP commands and events are protocol-level interfaces; a session does not make an unsupported command valid.
  • No event arrives: Check the event name and whether the relevant protocol domain needs enabling. The documented Animation example calls Animation.enable before listening; that example does not establish identical setup requirements for all domains.
  • A send fails after cleanup: A detached session cannot send messages. Create and use a valid session for the work rather than retaining a detached one.
  • A protocol call takes longer than expected: Puppeteer’s version 25.12.0 ConnectOptions reference documents protocolTimeout as the timeout for individual protocol calls, with a default of 180,000 milliseconds. This is a browser launch/connect configuration option, not a per-send() argument. ConnectOptions reference
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 is a screenshot API and MCP server for developers. A single GET request can return a screenshot or PDF, without requiring you to create and manage a Puppeteer browser session.

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

For example, this cURL request saves a WebP screenshot of Stripe. See the ScreenshotNeo API documentation for request options.

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

ScreenshotNeo accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; those steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. Its MCP server offers 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 shots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Frequently Asked Questions

Can I create a CDPSession directly with its constructor?

No. Puppeteer documents the constructor as internal; create a session through Puppeteer’s page or target methods.

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

Does every CDP domain need an enable command before listening?

The Animation example enables its domain, but the documentation does not establish that every domain has the same requirement. Check the setup for the protocol feature you are using.

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 *

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.

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
Crashes, No Sound, or Screen Glitches?Free driver scan

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.