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.
MCP
安装 chrome-devtools-mcp,一个基于 Puppeteer 的 MCP 服务器,用于浏览器自动化和调试。
🌐 Install chrome-devtools-mcp,
a Puppeteer-based MCP server for browser automation and debugging.
示例
🌐 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 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();