puppeteer/puppeteer
GitHub: puppeteer/puppeteer
Puppeteer 是一个提供 Chrome/Firefox 浏览器编程控制能力的 Node.js 库,用于实现浏览器自动化测试、网页抓取和服务端渲染等场景。
Stars: 95077 | Forks: 9454
# Puppeteer
[](https://github.com/puppeteer/puppeteer/actions/workflows/ci.yml)
[](https://npmjs.org/package/puppeteer)
## 安装
```
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.
```
:::note
现代的包管理器(包括 npm(参见 [RFC](https://github.com/npm/rfcs/pull/868))、pnpm、Yarn、Bun 和 Deno)默认会阻止依赖项的安装脚本。如果安装脚本被阻止,Puppeteer 将不会在安装期间下载浏览器,从而导致运行时错误。
你可以在安装后通过运行以下命令来手动下载所需的浏览器:
```
npx puppeteer browsers install
```
或者,你可以配置你的包管理器以允许安装脚本运行(例如,使用 npm 时,通过在 `package.json` 的 `"allowScripts"` 中添加 `"puppeteer"`)。
:::
## MCP
安装 [`chrome-devtools-mcp`](https://github.com/ChromeDevTools/chrome-devtools-mcp),
这是一个基于 Puppeteer 的 MCP server,用于浏览器自动化和调试。
Puppeteer 还支持实验性的 [WebMCP](https://pptr.dev/guides/webmcp) API。
## 示例
```
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();
```
## 安装
```
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.
```
:::note
现代的包管理器(包括 npm(参见 [RFC](https://github.com/npm/rfcs/pull/868))、pnpm、Yarn、Bun 和 Deno)默认会阻止依赖项的安装脚本。如果安装脚本被阻止,Puppeteer 将不会在安装期间下载浏览器,从而导致运行时错误。
你可以在安装后通过运行以下命令来手动下载所需的浏览器:
```
npx puppeteer browsers install
```
或者,你可以配置你的包管理器以允许安装脚本运行(例如,使用 npm 时,通过在 `package.json` 的 `"allowScripts"` 中添加 `"puppeteer"`)。
:::
## MCP
安装 [`chrome-devtools-mcp`](https://github.com/ChromeDevTools/chrome-devtools-mcp),
这是一个基于 Puppeteer 的 MCP server,用于浏览器自动化和调试。
Puppeteer 还支持实验性的 [WebMCP](https://pptr.dev/guides/webmcp) API。
## 示例
```
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();
```
标签:CMS安全, JavaScript, MITM代理, SOC Prime, Web自动化, 开发工具, 数据可视化, 无头浏览器, 浏览器自动化, 自动化攻击