What programming languages does Playwright support?

Playwright is a browser automation library from Microsoft. It provides a high-level API to control web browsers and is designed to automate the broad and growing set of web browser capabilities used by Single Page Apps and Progressive Web Apps.

Playwright supports the following programming languages:

  1. JavaScript
  2. TypeScript
  3. Python
  4. C#
  5. Java

Here's a brief on how to use Playwright in each of these languages:

JavaScript/TypeScript:

In JavaScript or TypeScript, you can install Playwright using npm and use it as follows:

const playwright = require('playwright');  // JavaScript
// import { chromium } from 'playwright'; // TypeScript

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

Python:

In Python, you can install Playwright using pip and use it as follows:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context()
    page = context.new_page()
    page.goto('http://example.com')
    # other actions...
    browser.close()

C#:

In C#, you can install Playwright using NuGet and use it as follows:

using Microsoft.Playwright;

class Program
{
    public static async Task Main()
    {
        using var playwright = await Playwright.CreateAsync();
        var browser = await playwright.Chromium.LaunchAsync();
        var context = await browser.NewContextAsync();
        var page = await context.NewPageAsync();
        await page.GotoAsync('http://example.com');
        // other actions...
        await browser.CloseAsync();
    }
}

Java:

In Java, you can add the Playwright dependency in your pom.xml file and use it as follows:

import com.microsoft.playwright.*;

public class Example {
  public static void main(String[] args) {
    try (Playwright playwright = Playwright.create()) {
      Browser browser = playwright.chromium().launch();
      Page page = browser.newPage();
      page.navigate('http://example.com');
      // other actions...
      browser.close();
    }
  }
}

Each of these languages has its own Playwright client maintained by the Playwright team, ensuring that the API is idiomatic for each language and that the clients are kept up-to-date with the latest Playwright features.

Related Questions

Get Started Now

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