Does your company rely on browser automation or web scraping? We have a wild offer for our early customers! Read more →

Playwright Guide

Playwright is one of the most popular automation libraries created by Microsoft, with over 65k stars on Github.

Getting Started with a New Project

To begin, install Playwright

1
npm install playwright-core

Use chromium.connectOverCDP method to connect to your remote browser. Here's an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { chromium } from 'playwright-core'

(async () => {
  const rebrowserParams = {
    apiKey: '<YOUR_API_KEY>',
  }
  const browser = await chromium.connectOverCDP(`wss://ws.rebrowser.net/?${new URLSearchParams(rebrowserParams)}`)
  const context = browser.contexts()[0]
  const page = await context.newPage()
  await page.goto('https://rebrowser.net')
  await page.close()

  await (await browser.newBrowserCDPSession()).send('Rebrowser.finishRun')
  await browser.close()
})().catch((error) => console.error(error.message))

Now you can visit Dashboard / Runs and see the results of your run.

Make sure to replace YOUR_API_KEY with your actual API key.

You can read more about all parameters to start a new run on Documentation / API / startRun.

Updating Your Existing Code

If you have an existing codebase working with Playwright, it's super easy to modify it to start leveraging a remote browser instead.

Without Rebrowser

1
2
3
4
5
6
import playwright from 'playwright'

const browser = await playwright.chromium.launch()
const context = await browser.newContext()
const page = await context.newPage()
// ...

With Rebrowser

1
2
3
4
5
6
7
8
9
import { chromium } from 'playwright-core'

const rebrowserParams = {
  apiKey: 'YOUR_API_KEY',
}
const browser = await chromium.connectOverCDP(`wss://ws.rebrowser.net/?${new URLSearchParams(rebrowserParams)}`)
const context = browser.contexts()[0]
const page = await browser.newPage()
// ...

Playwright Python example

Python usage is quite similar to Node.js, we're gonna use connect_over_cdp. You just need to replace chromium.launch with chromium.connect_over_cdp in your existing code.

Take a look on the example below.

1
2
3
4
5
6
7
8
9
10
11
12
import asyncio
from rebrowser_playwright.async_api import async_playwright

async def main():
  async with async_playwright() as p:
    browser = await p.chromium.connect_over_cdp("wss://ws.rebrowser.net/?apiKey=YOUR_API_KEY")
    page = await browser.new_page()
    await page.goto("https://bot-detector.rebrowser.net/")
    # your business logic
    await browser.close()

asyncio.run(main())

How about Playwright Java and .NET?

All other programming language versions of Playwright support connectOverCDP. You just need to change one line of code to start using Rebrowser.

Get more info in Playwright docs: Java, .NET.

Fix The Leaks!

We highly encourage every customer to do some extra steps to fix all automation leaks.

You can read more on this page: Documentation / Stealth & Automation Detection