Puppeteer:启动浏览器进程失败!产卵

bib*_*scy 3 node.js web-scraping express puppeteer

当我尝试运行时node app.js,出现错误:

消息是无法启动浏览器进程!spawn /Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app

EACCES

我做了什么, 我检查了文件夹,/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app但文件没有压缩。它可以运行。

注意: 如果我尝试在没有路径的情况下执行,它可以工作,但我想使用 Chrome 或 Chromium 打开一个新页面。 const browser = await puppeteer.launch({headless:false'});

const express = require('express');
const puppeteer = require('puppeteer');
const app = express();

(async () => {
  const browser = await puppeteer.launch({headless:false, executablePath:'/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app'});
 const page = await browser.newPage();
 await page.goto('https://google.com', {waitUntil: 'networkidle2'});

})().catch((error) =>{
  console.error("the message is " + error.message);
});

app.listen(3000, function (){
    console.log('server started');
})
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

the*_*ton 9

如果您chrome://version/在这个确切的浏览器中导航到页面,它将显示Executable Path您需要用作executablePathpuppeteer 启动选项的确切字符串。

通常 chrome 的路径在 MAC 上是这样的:

/Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Run Code Online (Sandbox Code Playgroud)

如果chromium位于您的node_modules文件夹中,或者类似这样的事情:

/Users/iliebogdanbarbulescu/Downloads/firstProject/node_modules/chromium/lib/chromium/chrome-mac/Chromium.app/Contents/MacOS/Chromium
Run Code Online (Sandbox Code Playgroud)

现在,如果您比较用于的字符串executablePath:它与使用上述方法检索的字符串不同。应该将确切/Contents/MacOS/Chromium地添加到当前路径的末尾以使其工作。

注意:与 puppeteer 捆绑在一起的 Chromium 是与实际 pptr 版本一起使用的版本:如果您打算使用其他基于 chrome/或chromium 的浏览器,您可能会遇到意外问题。


Ber*_*rtC 9

跟进@theDavidBarton:

Puppeteer 附带的 Chromium 无法运行,但我的 MacBook 上安装的 Chrome 可以运行。

操作系统:OS-X 10.15.7 (Catalina) 节点版本:v14.5.0

失败代码:

const browser = await puppeteer.launch({
  headless: true, 
  executablePath: "/users/bert/Project/NodeJS/PuppeteerTest/node_modules/puppeteer/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium"
});
Run Code Online (Sandbox Code Playgroud)

成功代码:

const browser = await puppeteer.launch({
  headless: true, 
  executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
});
Run Code Online (Sandbox Code Playgroud)

完整代码,只是 Puppeteer 网站上的第一个示例:

const puppeteer = require('puppeteer');

(async () => {
    try {
        const browser = await puppeteer.launch({headless: true, executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"});
        const page = await browser.newPage();
        await page.goto('https://example.com');
        await page.screenshot({path: 'example.png'});

        await browser.close();
    } catch (err) {
        console.log(err);
    }
})();
Run Code Online (Sandbox Code Playgroud)

而且,是的,我得到了屏幕截图!:-)


Alb*_*ves 6

使用位置 chrome: https: //www.npmjs.com/package/locate-chrome

const locateChrome = require('locate-chrome');
const executablePath = await new Promise(resolve => locateChrome(arg => resolve(arg)));
const browser = await puppeteer.launch({ executablePath });
Run Code Online (Sandbox Code Playgroud)