YBFACC / blog

仅记录个人学习使用
3 stars 0 forks source link

安装puppeteer踩坑 #28

Open YBFACC opened 4 years ago

YBFACC commented 4 years ago

安装puppeteer踩坑

使用npm安装npm i puppeteer

执行以下代码

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

报错 Error: Could not find browser revision 768783. Run "npm install" or "yarn install" to download a browser binary.

需要手动安装Chromium

下载地址:https://download-chromium.appspot.com/

然后更改代码指向Chromium.app路径

const puppeteer = require('puppeteer')

;(async () => {
  const browser = await puppeteer.launch({
    executablePath: '/Applications/Chromium.app',
    headless: false
  })
  const page = await browser.newPage()
  await page.goto('https://example.com')
  await page.screenshot({ path: 'example.png' })

  await browser.close()
})()

报错 UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process! spawn /Applications/Chromium.app/ EACCES

更改路径 executablePath: '/Applications/Chromium.app/Contents/MacOS/Chromium'

const puppeteer = require('puppeteer')

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

  await browser.close()
})()

参考的解决方案

puppeteer新手遇到的坑