You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何消除Puppeteer旧Headless模式弃用警告?

消除Puppeteer旧Headless模式弃用警告的方案

方案一:调整Puppeteer启动配置

你的代码里已经设置了headless: false(使用带界面的Chrome),但部分版本的Puppeteer仍会触发该警告,可通过以下方式修改配置解决:

  • 修改启动配置,添加ignoreDefaultArgs: ['--headless'],阻止默认Headless参数注入:
const bot = require("puppeteer");
const fs = require("fs");
const botConfiguration ={
  headless:false, 
  executablePath:"C:/Program Files/Google/Chrome/Application/chrome.exe",
  userDataDir: "C:/Users/bot/AppData/Local/Google/Chrome/User Data/",
  args: ['--profile-directory=Profile 28'],
  ignoreDefaultArgs: ['--headless'] // 新增该行以消除警告
};
async function runBot() {
  const chromeBrowser = await bot.launch(botConfiguration)
  const chromeBrowserPage = await chromeBrowser.newPage()
  await chromeBrowserPage.goto("https://www.google.com/")
};
runBot();

如果后续需要切换到无界面模式,直接将headless设置为"new"即可,既启用新Headless模式,也不会触发警告:

const botConfiguration ={
  headless:"new", // 启用新无界面模式
  executablePath:"C:/Program Files/Google/Chrome/Application/chrome.exe",
  userDataDir: "C:/Users/bot/AppData/Local/Google/Chrome/User Data/",
  args: ['--profile-directory=Profile 28']
};

方案二:屏蔽Node.js警告输出

不想改代码的话,可通过以下方式屏蔽警告:

  • 启动脚本时添加环境变量:
NODE_NO_WARNINGS=1 node 你的脚本文件名.js
  • 或者在代码开头添加全局屏蔽逻辑:
process.removeAllListeners('warning');

内容的提问来源于stack exchange,提问作者jaytheblogger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 07:10:57