How can I run Playwright in headless mode?

Playwright is a Node library used to automate the Chrome, Safari, and Firefox browsers. This library provides a high-level API to control these browsers. Playwright can run in headless mode, which means it can perform actions in the browser without showing the browser UI.

To run Playwright in headless mode, you have to set the headless option to true when launching the browser. By default, Playwright runs in headless mode.

Here is how you can do it:

In JavaScript:

const playwright = require('playwright');

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

In Python:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://example.com")
    browser.close()

In both examples, the headless option is set to true which means the browser will run in headless mode. If you want to show the browser UI, you can set the headless option to false.

Related Questions

Get Started Now

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