mac使用puppeteer报错,UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!

(node:63185) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! spawn /Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer-core/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium ENOENT


TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md

    at onClose (/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
    at ChildProcess.<anonymous> (/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:185:85)
    at ChildProcess.emit (events.js:315:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
    at onErrorNT (internal/child_process.js:465:16)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:63185) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:63185) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

问题原因,没有在默认路径下找到Chromium。

解决步骤:
断点调试
跟踪到文件

/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserFetcher.js

找到这一行

        const outputPath = this._getFolderPath(revision);

打印输出outputPath,结果为

‌/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer-core/.local-chromium/mac-818858

再结合程序报道那个错误,其中给出了完整的路径

/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer-core/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium

可以发现.local-chromium往后就没有东西了。所以程序找不到是理所当然了。

接下来就是查找chromium到底在什么位置,通过以下代码可以得到它的路径。

const puppeteer = require("puppeteer");
const browserFetcher = puppeteer.createBrowserFetcher();

const run=async ()=>{
  const res=await browserFetcher.download("818858") //程序第一次报错会显示这个版本号
  console.log(res)
}
run()

得到信息

{
  revision: '818858',
  executablePath: '/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer/.local-chromium/mac-818858/chrome-mac/Chromium.app/Contents/MacOS/Chromium',
  folderPath: '/Users/chennan/xxxx/crawler-core/webapp/node_modules/puppeteer/.local-chromium/mac-818858',
  local: true,
  url: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/818858/chrome-mac.zip',
  product: 'chrome'
}

通过executablePath就知道了文件的位置。

然后补全之前的路径即可。

posted @ 2021-03-31 22:19  公众号python学习开发  阅读(1995)  评论(1编辑  收藏  举报