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

How to Get a Connection from a Puppeteer CDPSession

Call session.connection() to access a Puppeteer CDPSession’s underlying connection, and check for undefined before using it.

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.

Call session.connection() to get the underlying Puppeteer connection. The method returns Connection | undefined, so check for undefined before using the result.

Get the connection from an existing CDPSession

Use the documented connection() accessor:

const connection = client.connection();
if (!connection) {
  throw new Error('This CDPSession has no underlying connection');
}

The return value is optional: Puppeteer documents it as the underlying connection “if any.” Do not assume every session has one. See the CDPSession.connection() API reference for the method signature matching the documentation version reviewed on October 3, 2026; consult the API reference for the version installed in your project if it differs.

As an Amazon Associate I earn from qualifying purchases.

Create a session from a page first

If you have a Puppeteer page rather than a session, create a page-attached CDP session and then call the same accessor:

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

if (!connection) {
  throw new Error('This CDPSession has no underlying connection');
}

Page.createCDPSession() returns a Promise<CDPSession>. The official Page.createCDPSession() documentation describes creating a session attached to the page.

What the session and connection are for

A CDPSession is Puppeteer’s interface for communicating with the raw Chrome DevTools Protocol. Use send() to issue protocol commands and on() to listen for protocol events. Obtain sessions through Puppeteer APIs; the CDPSession constructor is marked internal, so application code should not instantiate or subclass it. See Puppeteer’s CDPSession API source.

Puppeteer’s implementation also exposes Connection.fromSession(session), which delegates to session.connection(). For ordinary application code, calling the documented method directly is the simple route; there is no need to reach into a private field. The Connection implementation shows that delegation.

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

Handle a missing connection safely

Because connection() may return undefined, narrow the type before calling methods on the result. Throwing, returning an optional result, or taking a fallback path are all reasonable choices depending on whether a connection is required for the operation:

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

if (connection === undefined) {
  return; // Or handle the session without a connection.
}

// Use documented Connection methods here.

The accessor documents the value it returns, but it does not promise that every internal property or behavior of Connection is stable. Prefer its documented public API. If your code depends on implementation details, verify them against the exact Puppeteer version in your dependency lockfile and consult that version’s documentation.

Troubleshooting

  • TypeScript says the connection may be undefined: That is expected. Check for undefined before using the value, or use an explicit guard that throws when your code requires a connection.
  • You have a page, not a session: Call await page.createCDPSession() first, then call connection() on the returned session.
  • You are trying to construct a CDPSession yourself: Its constructor is internal. Get the session from a Puppeteer API such as page.createCDPSession() instead.
  • Code relies on undocumented connection internals: Check the API and source for your installed Puppeteer version. Repository main can change and is not a guarantee for a different release.
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 the goal is simply to capture a website rather than use the Chrome DevTools Protocol directly, ScreenshotNeo offers a one-request screenshot API:

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 request options. Before a capture, it accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; these 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. ScreenshotNeo also provides an MCP server with tools for AI agents, including take_screenshot, get_page_info, and capture_pdf.

The free plan includes 1,000 screenshots per month with no card required; paid plans start at $5 for 3,000 screenshots. Learn more at ScreenshotNeo or 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.
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

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.