October 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 PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

How to Send Commands with Puppeteer CDPSession

Use Puppeteer’s Page.createCDPSession() to create a CDP session, send commands with client.send(), handle events, and detach when finished.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In current Puppeteer code, create a Chrome DevTools Protocol session with await page.createCDPSession(), send commands with await client.send('Domain.command', params), and detach the session when you are done. The examples below follow Puppeteer’s documented API, identified as version 25.12.0 for these methods.

Send a command through a page CDP session

A CDPSession sends raw Chrome DevTools Protocol (CDP) commands to the browser target associated with a Puppeteer page. Command calls are asynchronous: await send() to receive the response or catch a rejected Promise.

As an Amazon Associate I earn from qualifying purchases.

import puppeteer from 'puppeteer';

const browser = await puppeteer.launch();
let client;

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

  await client.send('Runtime.enable');
  const result = await client.send('Runtime.evaluate', {
    expression: '2 + 2',
    returnByValue: true,
  });

  console.log(result.result.value); // 4
} finally {
  if (client) await client.detach();
  await browser.close();
}

Runtime.enable enables that protocol domain. The second call evaluates an expression and asks for its value to be returned by value; with this response shape, the example prints 4. Check the protocol documentation for the browser version you run when choosing commands, parameters, and response fields.

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

Create the session with the current Page API

Use await page.createCDPSession(). Puppeteer’s documented signature is page.createCDPSession(): Promise<CDPSession>. The creation call itself is asynchronous, so await it before sending commands.

Avoid using page.target().createCDPSession() in new code. Puppeteer marks Page.target() obsolete and recommends Page.createCDPSession() instead.

Send commands and handle their results

The documented send() method accepts a protocol method name, optional parameters, and optional command options, and returns a Promise for that command’s response. In current Puppeteer’s TypeScript protocol mapping, the method string constrains the corresponding parameter and return types:

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
const response = await client.send('Domain.command', {
  // Parameters required by the chosen protocol command
});

Domain.command is a placeholder, not a runnable CDP method. Replace it and the parameters with a command supported by the Chrome version you use. For a command that accepts no parameters, omit the second argument.

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.

In TypeScript, the documented generic signature ties the command name to its mapped parameter and return types: client.send<T extends keyof ProtocolMapping.Commands>(method, params?, options?). These types help with editor completion, but they do not guarantee that every command is available or behaves identically in every browser release.

Listen for CDP events

To receive events, enable the relevant protocol domain, register a listener, and then perform the action that may emit the event. Puppeteer’s CDPSession reference demonstrates this pattern with the Animation domain:

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

// Perform an action on the page that may create an animation.

Use the event name and event payload expected by the protocol version in use. Attach the listener before triggering the page action so you do not miss an event emitted earlier.

Rank #4
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

Detach the session when finished

Call await client.detach() when the session is no longer needed. After detaching, the session will not emit events and cannot send messages. If your code may throw, put cleanup in a finally block, as in the first example, and close the browser separately.

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

Common problems and fixes

  • The session creation call is missing or not awaited: create it with const client = await page.createCDPSession() before calling send().
  • A command rejects or its response shape differs from the example: verify the method, required parameters, and returned fields against the CDP protocol for the Chrome version paired with your Puppeteer installation. The sample’s Runtime.evaluate response is specific to the options shown.
  • Events do not arrive: enable the relevant domain and register the listener before the action that should emit the event.
  • Sending fails after cleanup: a detached session cannot send commands or emit events. Create a new session if you need to continue working.
  • Older code uses page.target(): migrate session creation to page.createCDPSession(), the current documented Page method.
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 sending arbitrary CDP commands, ScreenshotNeo provides a one-request screenshot API and an MCP server. It accepts cookie and consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each cleanup step can be turned off. Bot checks and CAPTCHAs, blank pages, timeouts, failed loads, and cache hits cost nothing, and responses identify the page verdict and billing status in headers.

For a screenshot, use this cURL call; replace the example URL with the page you need and provide your API key. 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

The same endpoint is available from Python or Node.js:

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 shots. Sign up for free ScreenshotNeo screenshots.

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
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair 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.