Skip to content

Playwright Integration

Playwright is a modern browser automation library that supports Chromium, Firefox, and WebKit.

Installation

bash
# Python
pip install playwright
playwright install

# Node.js
npm install playwright

Python Example

Basic Usage

python
from playwright.sync_api import sync_playwright

def run(playwright):
    # Configure proxy
    browser = playwright.chromium.launch(
        proxy={
            'server': 'http://proxy.okkproxy.com:8080',
            'username': 'your_username',
            'password': 'your_password'
        }
    )

    page = browser.new_page()
    page.goto('https://example.com')

    # Check IP
    page.goto('https://ipinfo.io')
    print(page.content())

    browser.close()

with sync_playwright() as playwright:
    run(playwright)

Async Example

python
import asyncio
from playwright.async_api import async_playwright

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(
            proxy={
                'server': 'http://proxy.okkproxy.com:8080',
                'username': 'your_username',
                'password': 'your_password'
            }
        )

        page = await browser.new_page()
        await page.goto('https://example.com')

        await browser.close()

asyncio.run(main())

Node.js Example

javascript
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'http://proxy.okkproxy.com:8080',
      username: 'your_username',
      password: 'your_password'
    }
  });

  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Your code here

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

Advanced Features

Multiple Contexts

python
browser = playwright.chromium.launch()

# Context 1 with proxy A
context1 = browser.new_context(
    proxy={'server': 'http://proxy1.okkproxy.com:8080'}
)

# Context 2 with proxy B
context2 = browser.new_context(
    proxy={'server': 'http://proxy2.okkproxy.com:8080'}
)

Stealth Mode

python
context = browser.new_context(
    proxy={'server': 'http://proxy.okkproxy.com:8080'},
    user_agent='Mozilla/5.0 ...',
    viewport={'width': 1920, 'height': 1080},
    locale='en-US',
    timezone_id='America/New_York'
)

Best Practices

  1. Use contexts for isolation
  2. Handle timeouts properly
  3. Implement error handling
  4. Close resources properly

Resources