How to use the Playwright API?

Playwright is a Node.js library to automate browsers, which includes Chrome, Firefox, and Webkit, providing a high-level API to control them. It enables you to automate browser tasks such as generating PDFs, taking screenshots, testing web applications, and, importantly, web scraping.

Installation

You can install Playwright using npm:

npm i playwright

This command will install Playwright and browser binaries for Chromium, Firefox, and WebKit.

Example

Here's a simple example of how to use Playwright:

const playwright = require('playwright');

(async () => {
  const browser = await playwright.chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goto('http://example.com');
  await page.screenshot({ path: 'example.png' });

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

In this example, we first import the Playwright module. Then, we launch a new Chromium browser, create a new browser context (which is like a fresh browser profile), and open a new page. We then navigate to 'http://example.com' and take a screenshot of the page, saving it as 'example.png'. Finally, we close the browser.

API Overview

Here's an overview of some of the key API methods:

  • playwright.chromium.launch(): Launches a new Chromium browser.
  • browser.newContext(): Creates a new browser context.
  • context.newPage(): Opens a new page in the browser context.
  • page.goto(url): Navigates to the specified URL.
  • page.screenshot(options): Takes a screenshot of the page. Options can include path (the path to save the screenshot), type (the image format, e.g., 'png' or 'jpeg'), and fullPage (whether to take a screenshot of the full page).
  • browser.close(): Closes the browser.

These are just some of the methods available in Playwright's API. You can also interact with page elements, fill in and submit forms, listen for browser events, and more. You can find full API documentation on the Playwright website.

Related Questions

Get Started Now

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