新的chrome headless模式 headless=new

3,035 阅读1分钟

今天升级Puppeteer到20的版本,发现有一个警告,提示headless有问题

 Puppeteer old Headless deprecation warning:
    In the near feature `headless: true` will default to the new Headless mode
    for Chrome instead of the old Headless implementation. For more
    information, please see https://developer.chrome.com/articles/new-headless/.
    Consider opting in early by passing `headless: "new"` to `puppeteer.launch()`
    If you encounter any bugs, please report them to https://github.com/puppeteer/puppeteer/issues/new/choose.

image.png

打开链接发现新版的chrome对headless有全新的升级。大概意思就是以前的headless的设计和headful是分离的,导致正常模式和无头模式下出现不同的问题,且难以解决,尤其是有扩展插件的情况,新的headless=new模式,将他们合并到一起了,这样两个模式下就完全一致了。

image.png

Puppeteer使用新参数例子: 原来的headless参数默认是true,那么新的需要手动传入new,如果是false则不变

const PCR = require('puppeteer-chromium-resolver');
const stats = await PCR({});
const browser = await stats.puppeteer.launch({
  headless: 'new',
  // `headless: true` (default) enables old Headless;
  // `headless: 'new'` enables new Headless;
  // `headless: false` enables “headful” mode.
});

由于统一了模式,那么比如像扩展插件,打印pdf,截图,等等都可以实现两端一致,大家可以试试

链接:Chrome’s Headless mode gets an upgrade: introducing --headless=new - Chrome Developers