cloudflare-puppeteer
repository·main·Indexed 18 days ago
https://github.com/cloudflare/puppeteerA specialized fork of Puppeteer core optimized for Cloudflare Browser Run, providing a lightweight version for use within Cloudflare Workers. It utilizes the standard Chrome DevTools Protocol (CDP) for communication and includes tools such as @puppeteer/browsers for browser installation, @puppeteer/ng-schematics for Angular E2E testing, and @pptr/testserver for local HTTP/HTTPS testing servers.
What's inside cloudflare-puppeteer
- Puppeteer is a Node.js library that provides a high-level API to control Chrome or Firefox. It communicates via the DevTools Protocol or WebDriver BiDi. By default, Puppeteer runs in 'headless' mode (no visible UI), but it can be configured to run in 'headful' mode (visible browser).
Programmatic API for @puppeteer/browsers
mainThe
@puppeteer/browsersprogrammatic API allows you to install, launch, and manage browsers directly from your code.Core Functionality
- Installation: Use
install(options)to download browsers anduninstall(options)to remove them. UsecanDownload(options)to check availability. - Launching: Use
launch(opts)to start a browser instance. Note that launching system browsers is only supported for Chrome/Chromium. - Path Management: Use
computeExecutablePath(options)for managed browsers orcomputeSystemExecutablePath(options)for system-installed browsers. - Discovery: Use
getInstalledBrowsers(options)to retrieve metadata about browsers currently in your cache directory. - Profiles: Use
createProfile(browser, opts)to manage browser profiles.
Key Interfaces
When calling these functions, you will interact with several option interfaces:
InstallOptionsLaunchOptionsUninstallOptionsGetInstalledBrowsersOptionsProfileOptionsSystemOptions
- Installation: Use
Use @cloudflare/puppeteer with Cloudflare Browser Run
mainThis repository is a specialized fork of Puppeteer core designed for use with Cloudflare Browser Run (formerly Browser Rendering). It aims to minimize library size for Workers and provide a seamless experience in the Cloudflare ecosystem.
Starting with
@cloudflare/puppeteerversion 1.1.0, the library uses the standard Chrome DevTools Protocol (CDP) to communicate with Browser Run. Most existing Puppeteer code should work without modification.Handle multiple interceptors and asynchronous resolutions safely
mainPuppeteer raises a
Request is already handled!exception ifabort,continue, orrespondare called more than once for the same request.Because 3rd party packages or other listeners might resolve a request while your handler is awaiting an asynchronous operation, you must follow these rules:
- Check status synchronously: Always call
request.isInterceptResolutionHandled()(orrequest.interceptResolutionState()) immediately before calling a resolution method. - Atomic execution: Execute the check and the resolution method (
abort/continue/respond) within the same synchronous code block to avoid race conditions. - Async handlers: If your handler is
async, you must re-verify the resolution status after everyawaitbefore proceeding with a resolution.
page.on('request', async interceptedRequest => { // 1. Initial check if (interceptedRequest.isInterceptResolutionHandled()) return; await someLongAsyncOperation(); // 2. Re-check after async operation if (interceptedRequest.isInterceptResolutionHandled()) return; interceptedRequest.continue(); });- Check status synchronously: Always call
Puppeteer vs Selenium WebDriver
mainPuppeteer is not a direct replacement for Selenium WebDriver, as they serve different primary purposes:
Feature Selenium WebDriver Puppeteer Primary Focus Cross-browser automation Chromium-based browsers Language Support Multiple languages JavaScript only Setup Requires driver configuration Zero setup; bundles compatible browser Architecture Request/Response Event-driven (reduces flakiness) Use Puppeteer when you want deep integration with Chromium features, high-speed execution, and an event-driven architecture that avoids the need for manual
sleep()calls.Understanding Puppeteer's browser versioning
mainPuppeteer treats itself and Chromium as an indivisible entity. Each version of Puppeteer bundles a specific version of Chromium that is guaranteed to work with it.
If you encounter compatibility issues, it is because Puppeteer is tied to a specific Chromium revision. You can find the specific Chrome version used by a Puppeteer release by checking the
chromeentry in therevisions.tsfile in the source repository.Manage BrowserContexts for isolated user sessions
mainA
BrowserContextrepresents individual user contexts within aBrowser.Key behaviors:
- Isolation: Each context has isolated storage, including cookies and
localStorage. - Creation: A
Browserhas a single context by default when launched. You can create additional isolated contexts usingBrowser.createBrowserContext(). - Popups: If a
Pageopens another page (e.g., viawindow.open), the popup belongs to the parent page'sBrowserContext.
- Isolation: Each context has isolated storage, including cookies and
Screenshot concurrency and method interference
mainPuppeteer manages certain operations to prevent interference while a screenshot is being captured within a
BrowserContext.Automatic Waiting: The following methods will automatically wait for an ongoing screenshot to finish before executing:
BrowserContext.newPage()Browser.newPage()Page.close()
No Automatic Waiting:
Page.bringToFront()does not wait for existing screenshot operations to complete.
Use configuration files in v19.1.0+
mainAs of version 19.1.0, Puppeteer supports the use of configuration files for setup and execution.How Puppeteer works in Chrome extensions
mainRunning Puppeteer in a Chrome extension environment is experimental and differs significantly from a standard Node.js environment.
Key Constraints
- CDP Access: Extensions access the Chrome DevTools Protocol via the
chrome.debuggerAPI, which provides restricted access and allows attaching to only one page at a time. - Transport: Puppeteer must use a specialized transport layer (
ExtensionTransport) instead of the standard Node.js transport. - Single Page Limitation: Puppeteer's view is limited to a single page (and its frames/workers). You cannot use Puppeteer to create new pages. To open a new page, you must use the
chrome.tabsAPI and then establish a new Puppeteer connection for that specific tab.
- CDP Access: Extensions access the Chrome DevTools Protocol via the
Understand the separation of puppeteer and puppeteer-core in v18.2.0+
mainSince version 18.2.0, the project is split into two packages:
puppeteer: The full package that includes both the API and the automatic downloading of a compatible browser (Chromium).puppeteer-core: A lightweight version that provides only the API and does not download any browsers. This is ideal for environments where you want to connect to an existing browser instance or manage downloads manually.
Experimental WebDriver BiDi support for Firefox in Puppeteer v21.6.0+
mainPuppeteer21.6.0introduced experimental support for the WebDriver BiDi protocol when using Firefox. This is an alternative to the CDP protocol and includes a BiDi implementation ofPuppeteer.connectfor Firefox.