📜  puppeeter mac m1 - Javascript (1)

📅  最后修改于: 2023-12-03 15:18:43.592000             🧑  作者: Mango

Puppeteer Mac M1 - Javascript

Puppeteer is a Node.js library that provides a high-level API for controlling headless Chrome or Chromium browsers. It is widely used for web scraping, automation testing, and creating bots.

However, if you're using a Mac with an M1 chip, you might encounter some issues when trying to run Puppeteer. In this article, we'll discuss how to set up and use Puppeteer on a Mac with an M1 chip.

Prerequisites

Before you start using Puppeteer on your Mac with an M1 chip, make sure you have the following prerequisites:

  • Node.js v14 or higher
  • Xcode Command Line Tools
  • Homebrew package manager
Installing Chromium

Puppeteer uses Chromium as its default browser. Unfortunately, the version of Chromium that Puppeteer installs using NPM is not compatible with Macs with M1 chips.

To install Chromium manually, you can use Homebrew. First, install Homebrew using the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, install Chromium using the following command:

brew install --cask chromium
Installing Puppeteer

Once you've installed Chromium, you can install Puppeteer using NPM:

npm install puppeteer

However, the version of Puppeteer that NPM installs is not compatible with Macs with M1 chips. To install a compatible version, you can use the following command:

npm install puppeteer-core@^9.1.1
Running Puppeteer

Now that you've installed Puppeteer and Chromium, you can use Puppeteer in your Javascript code as you would normally.

Here's an example script that navigates to Google and takes a screenshot:

const puppeteer = require('puppeteer-core');

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

Make sure to provide the executablePath option when launching Puppeteer, pointing it to the Chromium executable.

Conclusion

That's it! By following these steps, you should now be able to use Puppeteer on your Mac with an M1 chip. Keep in mind that this version of Puppeteer might not have all the features of the latest version, but it should work for most use cases.