Skip to main content
Version: 24.38.0

常见问题

🌐 FAQ

问:谁维护 Puppeteer?

🌐 Q: Who maintains Puppeteer?

Chrome 浏览器自动化团队维护这个库,但我们非常希望你能在这个项目上提供帮助和专业知识!请参阅我们的贡献指南

🌐 The Chrome Browser Automation team maintains the library, but we'd love your help and expertise on the project! See our contributing guide.

问:跨浏览器支持的状况如何?

🌐 Q: What is the status of cross-browser support?

从 Puppeteer v23.0.0 开始,Puppeteer 同时支持 Chrome 和 Firefox。

🌐 From Puppeteer v23.0.0 onwards Puppeteer provides support for both Chrome and Firefox.

为了自动化,Chrome Puppeteer 默认使用 Chrome DevTools 协议 (CDP),但它也可以使用 WebDriver BiDi 进行自动化,WebDriver BiDi 是自动化 Firefox 的默认方式。

🌐 To automate Chrome Puppeteer uses the Chrome DevTools Protocol (CDP) by default, but it can also be automated using WebDriver BiDi which is the default for automating Firefox.

要理解 API 支持的细微差别,请参阅我们的WebDriver BiDi 指南

🌐 To understand the subtle differences in API support refer to our WebDriver BiDi guide.

问:Puppeteer 支持 WebDriver BiDi 吗?

🌐 Q: Does Puppeteer support WebDriver BiDi?

从 Puppeteer v23.0.0 开始,Puppeteer 对 WebDriver BiDi 提供了生产就绪的支持,可用于自动化 Chrome 和 Firefox。

🌐 From Puppeteer v23.0.0 and up Puppeteer has production-ready support for WebDriver BiDi to automate both Chrome and Firefox.

问:Puppeteer 会继续支持 CDP 吗?

🌐 Q: Will keep Puppeteer supporting CDP?

我们不会停止支持通过 CDP 对 Chrome 进行自动化——尽管 Puppeteer 支持 WebDriver BiDi。为了不破坏依赖 CDP 的现有自动化,同时也为了继续支持 Chrome 独有且未通过 WebDriver BiDi 标准化的自动化使用场景。

🌐 We are not going to stop supporting automation of Chrome with CDP - despite Puppeteer's support for WebDriver BiDi. To not break existing automation relying on CDP, but also to keep enabling automation use-cases unique to Chrome and not standardized with WebDriver BiDi.

问:Puppeteer 的目标和原则是什么?

🌐 Q: What are Puppeteer’s goals and principles?

该项目的目标是:

🌐 The goals of the project are:

  • 提供一个参考实现,高亮 Chrome DevToolsWebDriver BiDi 协议的功能。
  • 扩大自动化跨浏览器测试的采用。
  • 帮助测试新的 DevTools 协议和 WebDriver BiDi 功能...并捕获错误!
  • 了解更多关于自动化浏览器测试的痛点,并帮助填补这些空白。

我们采用Chromium 原则来帮助我们推动产品决策:

🌐 We adapt Chromium principles to help us drive product decisions:

  • 速度:Puppeteer 对自动化页面几乎没有性能开销。
  • 安全性:Puppeteer 与浏览器是独立进程运行的,因此自动化可能存在恶意的页面是安全的。
  • 稳定性:Puppeteer 不应该不稳定,也不应该有内存泄漏。
  • 简洁性:Puppeteer 提供了一个高级 API,它易于使用、理解和调试。

问:Puppeteer 是 Selenium 的替代品吗?

🌐 Q: Is Puppeteer a replacement for Selenium?

Puppeteer 是一个基于 Node.js 的参考实现,用于展示如何使用 CDP 和 WebDriver BiDi 自动化浏览器——这是 Selenium 项目也在贡献的相同网页标准。

🌐 Puppeteer is a Node.js based reference implementation of how to automate browsers with CDP and WebDriver BiDi - the same web standard the Selenium project is also contributing to.

Selenium 项目在多个方面超越了 Puppeteer 的功能:它提供的不仅仅是 JavaScript 的绑定,还为更多语言提供支持,例如它还提供了用于大规模自动化的工具,如 Selenium Grid。这两者都超出了 Puppeteer 的范围。

🌐 The Selenium project goes beyond what Puppeteer offers in multiple aspects: it provides bindings for more languages than just JavaScript and for example it also offers tooling to orchestrate automation at large, like Selenium Grid. Both is beyond Puppeteer's scope.

有一些社区项目为 Puppeteer 添加了超出其核心功能的能力,使测试等事情更加方便。例如,参见:

🌐 There are community projects that add capabilities to Puppeteer beyond its core, making things like testing more convenient. For example see:

问:为什么 Puppeteer v.XXX 不适用于某个版本的 Chrome 或 Firefox?

🌐 Q: Why doesn’t Puppeteer v.XXX work with a certain version of Chrome or Firefox?

每个 Puppeteer 版本都会与特定的浏览器版本紧密打包,以确保与底层协议的实现兼容,包括 Chrome DevTools 协议和 WebDriver BiDi。

🌐 Every Puppeteer release is tightly bundled with a specific browser release to ensure compatibility with the implementation of the underlying protocols, the Chrome DevTools Protocol and WebDriver BiDi.

这是为了防止 ChromeFirefox 的更改意外破坏 Puppeteer。

🌐 This is to prevent changes in either Chrome or Firefox from unexpectedly breaking Puppeteer.

问:Puppeteer 使用哪个版本的 Chrome 和 Firefox?

🌐 Q: Which Chrome and Firefox version does Puppeteer use?

revisions.ts 中查找 chromefirefox 条目。

🌐 Look for the chrome and firefox entries in revisions.ts.

问:什么是“导航”?

🌐 Q: What’s considered a “Navigation”?

从 Puppeteer 的角度来看,“导航”是指任何会改变页面 URL 的行为。除了常规导航——浏览器访问网络以从 Web 服务器获取新文档之外,这还包括锚点导航历史记录 API的使用。

🌐 From Puppeteer’s standpoint, “navigation” is anything that changes a page’s URL. Aside from regular navigation where the browser hits the network to fetch a new document from the web server, this includes anchor navigations and History API usage.

按照“导航”的这个定义,Puppeteer 可以无缝地与单页应用配合工作。

🌐 With this definition of “navigation,” Puppeteer works seamlessly with single-page applications.

问:"受信任"输入事件和"不受信任"输入事件有什么区别?

🌐 Q: What’s the difference between a “trusted" and "untrusted" input event?

在浏览器中,输入事件可以分为两大类:可信事件与不可信事件。

🌐 In browsers, input events could be divided into two big groups: trusted vs. untrusted.

  • 可信事件:由用户与页面交互产生的事件,例如使用鼠标或键盘。
  • 不受信任的事件:由 Web API 生成的事件,例如 document.createEventelement.click() 方法。

网站可以区分这两组:

🌐 Websites can distinguish between these two groups:

  • 使用Event.isTrusted事件标志
  • 嗅探伴随事件。例如,每个可信的 'click' 事件之前都有 'mousedown''mouseup' 事件。

为了自动化的目的,生成受信任的事件很重要。所有使用 Puppeteer 生成的输入事件都是受信任的,并触发适当的伴随事件。 如果由于某种原因需要一个不受信任的事件,总是可以通过 page.evaluate 进入页面上下文并生成一个伪造事件:

🌐 For automation purposes it’s important to generate trusted events. All input events generated with Puppeteer are trusted and fire proper accompanying events. If, for some reason, one needs an untrusted event, it’s always possible to hop into a page context with page.evaluate and generate a fake event:

await page.evaluate(() => {
document.querySelector('button[type=submit]').click();
});

问:Puppeteer 支持媒体和音频播放吗?

🌐 Q: Does Puppeteer support media and audio playback?

Puppeteer 默认使用 Chrome for Testing 二进制文件,这些文件自 M120 起附带专有编解码器支持。

🌐 Puppeteer uses Chrome for Testing binaries by default which ship with proprietary codecs support starting from M120.

问:我在测试环境中安装/运行 Puppeteer 时遇到了问题。我应该在哪里寻求帮助?

🌐 Q: I am having trouble installing / running Puppeteer in my test environment. Where should I look for help?

我们有一个针对各种操作系统的故障排除指南,其中列出了所需的依赖。

🌐 We have a troubleshooting guide for various operating systems that lists the required dependencies.

问:我还有更多问题!我应该在哪里提问?

🌐 Q: I have more questions! Where do I ask?

有多种方法可以获取有关 Puppeteer 的帮助:

🌐 There are many ways to get help on Puppeteer:

在发布你的问题之前,请务必搜索这些渠道。

🌐 Make sure to search these channels before posting your question.