常见问题
¥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 实现自动化,这是 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 automations 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 开发工具 和 WebDriver 双向 协议功能的参考实现。
¥Provide a reference implementation that highlights the capabilities of the Chrome DevTools and WebDriver BiDi protocols.
-
扩大自动化跨浏览器测试的采用。
¥Grow the adoption of automated cross-browser testing.
-
帮助测试新的 DevTools 协议和 WebDriver BiDi 功能...并捕获错误!
¥Help dogfood new DevTools Protocol and WebDriver BiDi features...and catch bugs!
-
详细了解自动化浏览器测试的痛点并帮助填补这些空白。
¥Learn more about the pain points of automated browser testing and help fill those gaps.
我们采用 Chromium 原理 来帮助我们推动产品决策:
¥We adapt Chromium principles to help us drive product decisions:
-
速度:Puppeteer 相对于自动化页面的性能开销几乎为零。
¥Speed: Puppeteer has almost zero performance overhead over an automated page.
-
安全:Puppeteer 相对于浏览器而言是在进程外运行的,因此可以安全地自动执行潜在的恶意页面。
¥Security: Puppeteer operates off-process with respect to the browser, making it safe to automate potentially malicious pages.
-
稳定:Puppeteer 不应该是不稳定的,也不应该泄漏内存。
¥Stability: Puppeteer should not be flaky and should not leak memory.
-
简单:Puppeteer 提供了易于使用、理解和调试的高级 API。
¥Simplicity: Puppeteer provides a high-level API that’s easy to use, understand, and debug.
问:Puppeteer 是 Selenium 的替代品吗?
¥Q: Is Puppeteer a replacement for Selenium?
Puppeteer 是一个基于 Node.js 的参考实现,它介绍了如何使用 CDP 和 WebDriver BiDi 自动化浏览器 - Selenium 项目也为相同的 Web 标准做出贡献。
¥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.
这是为了防止 Chrome 或 Firefox 中的更改意外破坏 Puppeteer。
¥This is to prevent changes in either Chrome or Firefox not unexpectedly break Puppeteer.
问:Puppeteer 使用哪个版本的 Chrome 和 Firefox?
¥Q: Which Chrome and Firefox version does Puppeteer use?
在 revisions.ts 中查找 chrome
和 firefox
条目。
¥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.
问:“trusted"and"untrusted”输入事件有什么区别?
¥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.
-
可信事件:用户与页面交互生成的事件,例如 使用鼠标或键盘。
¥Trusted events: events generated by users interacting with the page, e.g. using a mouse or keyboard.
-
不受信任的事件:由 Web API 生成的事件,例如
document.createEvent
或element.click()
方法。¥Untrusted event: events generated by Web APIs, e.g.
document.createEvent
orelement.click()
methods.
网站可以区分这两组:
¥Websites can distinguish between these two groups:
-
使用
Event.isTrusted
事件标志¥using an
Event.isTrusted
event flag -
嗅探伴随事件。例如, 每个可信
'click'
事件之前都有'mousedown'
和'mouseup'
事件。¥sniffing for accompanying events. For example, every trusted
'click'
event is preceded by'mousedown'
and'mouseup'
events.
出于自动化目的,生成可信事件非常重要。使用 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();
});