Puppeteer
Puppeteer 是一个 JavaScript 库,提供了一个高级 API,用于通过 DevTools 协议 或 WebDriver BiDi 控制 Chrome 或 Firefox。 Puppeteer 默认在无头模式(无可见 UI)下运行
开始使用 | API | 常见问题 | 贡献 | 故障排除
🌐 Get started | API | FAQ | Contributing | Troubleshooting
安装
🌐 Installation
- npm
- Yarn
- pnpm
- Bun
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.
yarn add puppeteer # Downloads compatible Chrome during installation.
yarn add puppeteer-core # Alternatively, install as a library, without downloading Chrome.
pnpm add puppeteer # Downloads compatible Chrome during installation.
pnpm add puppeteer-core # Alternatively, install as a library, without downloading Chrome.
bun add puppeteer # Downloads compatible Chrome during installation.
bun add puppeteer-core # Alternatively, install as a library, without downloading Chrome.
现代包管理器(包括 npm(见 RFC)、pnpm、Yarn、Bun 和 Deno)默认会阻止依赖安装脚本。如果安装脚本被阻止,Puppeteer 在安装期间将不会下载浏览器,从而导致运行时错误。
🌐 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.
安装后,你可以通过运行以下命令手动下载所需的浏览器:
🌐 You can manually download the required browsers after installation by running:
- npm
- Yarn
- pnpm
- Bun
npx puppeteer browsers install
yarn dlx puppeteer browsers install
pnpm dlx puppeteer browsers install
bun x puppeteer browsers install
或者,你可以配置你的包管理器以允许安装脚本运行(例如,对于 npm,在你的 package.json 中将 "puppeteer" 添加到 "allowScripts")。
🌐 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).
MCP
安装 chrome-devtools-mcp,一个基于 Puppeteer 的 MCP 服务器,用于浏览器自动化和调试。
🌐 Install chrome-devtools-mcp,
a Puppeteer-based MCP server for browser automation and debugging.
Puppeteer 还支持实验性的 WebMCP API。
🌐 Puppeteer also supports the experimental 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();