puppeteer / puppeteer

puppeteer / puppeteer

Puppeteer is a JavaScript library which provides a high-level API to control Chrome or Firefox over the DevTools Protocol or WebDriver BiDi. Puppeteer runs in the headless (no visible UI) by default. Puppeteer 是一个 JavaScript 库,它提供了一套高级 API,用于通过 DevTools 协议或 WebDriver BiDi 控制 Chrome 或 Firefox。Puppeteer 默认在无头模式(无可见 UI)下运行。

Get started | API | FAQ | Contributing | Troubleshooting 快速开始 | API 文档 | 常见问题 | 贡献指南 | 故障排除

Installation

安装

npm i puppeteer # Downloads compatible Chrome during installation. npm i puppeteer # 安装时会自动下载兼容的 Chrome 浏览器。

npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome. npm i puppeteer-core # 或者,作为库安装,不下载 Chrome。

:::note Modern package managers (including npm (see the RFC), pnpm, Yarn, Bun, and Deno) block dependency install scripts by default. If the install script is blocked, Puppeteer will not download the browser during installation, leading to runtime errors. ::: :::note 现代包管理器(包括 npm(参见 RFC)、pnpm、Yarn、Bun 和 Deno)默认会阻止依赖项的安装脚本。如果安装脚本被阻止,Puppeteer 将无法在安装过程中下载浏览器,从而导致运行时错误。 :::

You can manually download the required browsers after installation by running: npx puppeteer browsers install 安装完成后,你可以通过运行以下命令手动下载所需的浏览器: npx puppeteer browsers install

Alternatively, you can configure your package manager to allow the install script to run (for example, with npm, by adding “puppeteer” to “allowScripts” in your package.json). 或者,你可以配置包管理器以允许运行安装脚本(例如,对于 npm,可以在 package.json 中将 “puppeteer” 添加到 “allowScripts” 中)。

MCP

MCP

Install chrome-devtools-mcp, a Puppeteer-based MCP server for browser automation and debugging. Puppeteer also supports the experimental WebMCP API. 安装 chrome-devtools-mcp,这是一个基于 Puppeteer 的 MCP 服务器,用于浏览器自动化和调试。Puppeteer 还支持实验性的 WebMCP API。

Example

示例

import puppeteer from 'puppeteer';
// Or import puppeteer from 'puppeteer-core';

// Launch the browser and open a new blank page.
const browser = await puppeteer.launch();
const page = await browser.newPage();

// Navigate the page to a URL.
await page.goto('https://developer.chrome.com/');

// Set the screen size.
await page.setViewport({width: 1080, height: 1024});

// Open the search menu using the keyboard.
await page.keyboard.press('/');

// Type into search box using accessible input name.
await page.locator('::-p-aria(Search)').fill('automate beyond recorder');

// Wait and click on first result.
await page.locator('.devsite-result-item-link').click();

// Locate the full title with a unique string.
const textSelector = await page
  .locator('::-p-text(Customize and automate)')
  .waitHandle();
const fullTitle = await textSelector?.evaluate(el => el.textContent);

// Print the full title.
console.log('The title of this blog post is "%s".', fullTitle);

await browser.close();
import puppeteer from 'puppeteer';
// 或者 import puppeteer from 'puppeteer-core';

// 启动浏览器并打开一个空白页面。
const browser = await puppeteer.launch();
const page = await browser.newPage();

// 导航到指定 URL。
await page.goto('https://developer.chrome.com/');

// 设置屏幕尺寸。
await page.setViewport({width: 1080, height: 1024});

// 使用键盘打开搜索菜单。
await page.keyboard.press('/');

// 使用无障碍输入名称在搜索框中输入内容。
await page.locator('::-p-aria(Search)').fill('automate beyond recorder');

// 等待并点击第一个结果。
await page.locator('.devsite-result-item-link').click();

// 使用唯一字符串定位完整标题。
const textSelector = await page
  .locator('::-p-text(Customize and automate)')
  .waitHandle();
const fullTitle = await textSelector?.evaluate(el => el.textContent);

// 打印完整标题。
console.log('这篇博客文章的标题是 "%s"。', fullTitle);

await browser.close();