Getting Started with Playwright

This article will walk you through the process of leveraging BrowserCat and Playwright for browser automation at scale.

Prerequisites

Before you get started, you’ll need to:

  1. Sign up for a free BrowserCat account.
  2. Create an API key.
  3. Install Playwright in your project…
npm install playwright
pip install playwright
mvn dependency:get -DgroupId=com.microsoft.playwright -DartifactId=playwright -Dversion=LATEST
dotnet add package Microsoft.Playwright

Your first automation

The following script connects to a BrowserCat browser, navigate to a website, and print the page title.

With a little tweaking (and the Playwright docs), you can automate literally anything you can do in a real browser.

import * as pw from 'playwright';

const bcatUrl = 'wss://api.browsercat.com/connect';

async function run() {
  // Create a new browser instance
  const browser = await pw.chromium.connect(bcatUrl, {
    headers: {'Api-Key': '<YOUR_API_KEY>'},
  });

  // Do stuff...
  const page = await browser.newPage();
  await page.goto('https://example.com');
  console.log(await page.title());

  // Close the browser when you're done
  await browser.close();
}

run();
import asyncio
from playwright.async_api import async_playwright, Playwright

bcatUrl = 'wss://api.browsercat.com/connect'

async def run():
    async with async_playwright() as p:
        # Create a new browser instance
        browser = await pw.chromium.connect(bcatUrl, headers={'Api-Key': '<YOUR_API_KEY>'})

        # Do stuff...
        page = await browser.new_page()
        await page.goto('https://example.com')
        print(await page.title())

        # Close the browser when you're done
        await browser.close()

asyncio.get_event_loop().run_until_complete(run())
package org.example;

import com.microsoft.playwright.*;

public class Main {
    public static void main(String[] args) {
        try (Playwright pw = Playwright.create()) {
            String bcatUrl = "wss://api.browsercat.com/connect";
            Browser browser = pw.chromium().connect(bcatUrl);

            Page page = browser.newPage();
            page.navigate("https://example.com");
            System.out.println(page.title());

            browser.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
using Microsoft.Playwright;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

class Program
{
    public static async Task Main()
    {
        try
        {
            var bcatUrl = "wss://api.browsercat.com/connect";
            using var pw = await Playwright.CreateAsync();
            await using var browser = await pw.Chromium.ConnectOverCDPAsync(bcatUrl);
            var page = await browser.NewPageAsync();
            await page.GotoAsync("https://example.com/", new PageGotoOptions { WaitUntil = WaitUntilState.NetworkIdle });
            Debug.WriteLine(await page.TitleAsync());
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.ToString());
        }
    }
}

Configure the browser

It’s really easy to configure our browsers for your needs. Whether you want to connect to a specific region, set up a third-party proxy, or customize your browser launch args, we’ve got you covered.

See out browser config docs for more info.