无头模式
🌐 Headless mode
默认情况下,Puppeteer 会以无头模式启动浏览器。
🌐 By default Puppeteer launches the browser in the Headless mode.
const browser = await puppeteer.launch();
// Equivalent to
const browser = await puppeteer.launch({headless: true});
在 v22 之前,Puppeteer 默认启动旧的无头模式。
旧的无头模式现在被称为 chrome-headless-shell,并作为单独的二进制文件发布。chrome-headless-shell 并不完全符合常规 Chrome 的行为,但在不需要完整 Chrome 功能集的自动化任务中性能更高。如果在你的使用场景中性能更为重要,请按照以下方式切换到 chrome-headless-shell:
🌐 Before v22, Puppeteer launched the old Headless mode by default.
The old headless mode is now known as
chrome-headless-shell
and ships as a separate binary. chrome-headless-shell does not match the
behavior of the regular Chrome completely but it is currently more performant
for automation tasks where the complete Chrome feature set is not needed. If the performance
is more important for your use case, switch to chrome-headless-shell as following:
const browser = await puppeteer.launch({headless: 'shell'});
要启动“有界面”的 Chrome 版本,请在启动浏览器时将 headless 设置为 false 选项:
🌐 To launch a "headful" version of Chrome, set the
headless to false
option when launching a browser:
const browser = await puppeteer.launch({headless: false});