Puppeteer可控制Chrome或Firefoxr的JS库

Puppeteer 是一个 JavaScript 库,它提供了高级 API,用于通过
DevTools 协议WebDriver BiDi 控制 Chrome 或 Firefox。
Puppeteer 默认在无头模式下运行(无可见 UI)。

开始 | API | 常见问题 | 贡献 | 故障排除

安装

bash npm2yarn 复制代码
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.

Example

ts 复制代码
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});

// Type into search box using accessible input name.
await page.locator('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('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();

关于项目

Puppeteer 是一个 JavaScript 库,它提供了高级 API,支持通过 DevTools 协议或 WebDriver BiDi 控制 Chrome 或 Firefox。Puppeteer 默认以无头模式(无可见 UI)运行。
Apache-2.0
Javascript
92,578
9314
1187
2017-05-10
2025-10-10

增长趋势 - stars