How can I run Puppeteer in 'headless' mode?

Puppeteer is a Node.js library that provides a high-level API for controlling Chrome or Chromium over the DevTools Protocol. Puppeteer can run in 'headless' mode, which means it can operate without a user interface or visual output.

Running Puppeteer in headless mode is quite straightforward. In the options object passed to puppeteer.launch(), you need to set headless to true.

Here's an example in JavaScript:

const puppeteer = require('puppeteer');

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

  // Perform any actions you want here...

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

In this example, puppeteer.launch({headless: true}) launches the browser in headless mode.

It's important to note that Puppeteer runs headless by default, but it can be configured to run 'full' (non-headless) Chrome or Chromium by setting headless to false.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({headless: false}); // default is true
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Perform any actions you want here...

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

In this second example, the browser will be launched in a way that allows you to see its UI.

Remember that running Puppeteer in 'headless' mode may lead to different results than 'headful' mode, due to the browser behaving differently. For example, a website may load different content or display a different layout depending on whether it detects a user interface.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon