Puppeteer无头模式启动失败求助:chromium路径ENOENT错误
Puppeteer无头模式启动失败求助
无法以无头模式运行Puppeteer,尝试多种方法(包括官方排查文档)仍未解决。刚接触编程,这个问题已经困扰我数周,恳请帮助。
我的代码
async function startBrowser() { const browser = await puppeteer.launch({ executablePath: '/path/to/chromium', headless: true, ignoreDefaultArgs: ['--disable-extensions'] }); const page = await browser.newPage(); return page; } const puppeteer = require('puppeteer-extra'); const StealthPlugin = require('puppeteer-extra-plugin-stealth') puppeteer.use(StealthPlugin()) const AdblockerPlugin = require('puppeteer-extra-plugin-adblocker') puppeteer.use(AdblockerPlugin({ blockTrackers: true })) const product_url = "https://www.zalando.se/nike-sportswear-air-flight-lite-mid-hoega-sneakers-whiteblack-ni112n02z-a11.html" const cart = "https://www.zalando.se/cart" async function addToCart(page){ // 访问商品页面 await page.goto(product_url) // 点击"handla"按钮 await page.waitForSelector("button[class*='DJxzzA'][class*='PLvOOB']"); await page.click("button[class*='DJxzzA'][class*='PLvOOB']"); // 点击Cookie弹窗的"OK" await page.waitForSelector("button.uc-btn-primary"); await page.click("button.uc-btn-primary"); // 选择EU 41尺码 await page.evaluate(() => { const sizeElems = document.getElementsByClassName('_6G4BGa'); if (sizeElems[4]) sizeElems[4].click(); }); console.log("运行中") await new Promise(r => setTimeout(r, 1000)); // 跳转到购物车 await page.goto(cart) // 点击"gå till checkout"按钮 await page.waitForSelector("button[class*='z-coast-base__sticky-sumary__cart__button-checkout']"); await page.click("button[class*='z-coast-base__sticky-sumary__cart__button-checkout']"); await new Promise(r => setTimeout(r, 1000)); // 输入邮箱 const email = await page.waitForSelector("[id='login.email']"); await email.type('marc.ehadsadwa@gmail.com'); await new Promise(r => setTimeout(r, 500)); // 输入密码 const password = await page.waitForSelector("[id='login.secret']"); await password.type('Bradasdwad'); await new Promise(r => setTimeout(r, 1000)); // 点击登录按钮 await page.evaluate(() => { const loginBtn = document.getElementsByClassName('DJxzzA')[0]; if (loginBtn) loginBtn.click(); }); await new Promise(r => setTimeout(r, 1000)); // 点击"hämta hos postombud" } async function checkout(){ var page = await startBrowser(); await addToCart(page); } checkout();
错误信息
Uncaught Error Error: Failed to launch the browser process! spawn /path/to/chromium ENOENT TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md at onClose (undefined:299:20) at <anonymous> (undefined:293:24) at emit (undefined:513:28) at ChildProcess._handle.onexit (undefined:289:12) at onErrorNT (undefined:476:16) at processTicksAndRejections (undefined:82:21)
解决方案
1. 修复Chromium路径问题
错误提示spawn /path/to/chromium ENOENT表示找不到指定路径的Chromium可执行文件,解决方法:
- 替换为实际路径:
- Windows:比如
C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe(注意转义反斜杠) - macOS:
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome - Linux:
/usr/bin/google-chrome或/usr/bin/chromium-browser
- Windows:比如
- 自动下载Chromium:直接删除
executablePath配置项,让puppeteer自动下载适配版本的Chromium,修改后的launch代码:const browser = await puppeteer.launch({ headless: true, ignoreDefaultArgs: ['--disable-extensions'] });
2. 调整无头模式参数
新版本Puppeteer的无头模式参数有更新,尝试将headless: true改为headless: 'new',适配新版无头模式:
const browser = await puppeteer.launch({ // 保留其他配置 headless: 'new' });
3. 检查依赖安装
确保所有依赖都正确安装,重新执行安装命令:
npm uninstall puppeteer-extra puppeteer-extra-plugin-stealth puppeteer-extra-plugin-adblocker npm install puppeteer-extra puppeteer-extra-plugin-stealth puppeteer-extra-plugin-adblocker
4. 权限与环境检查
- Linux/macOS用户:确保Chromium/Chrome有可执行权限,执行命令:
sudo chmod +x /path/to/your/chromium - Windows用户:以管理员身份运行命令行再执行脚本
代码小优化(可选)
原代码中部分选择器过长且冗余,替换为更稳定的部分类选择器(如[class*='xxx']),同时去掉page.click的多余回调参数,避免不必要的错误。
内容的提问来源于stack exchange,提问作者Marcnark
相关产品推荐
相关产品推荐

