Playwright Integration

Playwright Proxy SetupCross-browser automation with mobile IPs

Playwright's native proxy support makes it ideal for web automation. Configure SOCKS5 or HTTP proxies with authentication for Chromium, Firefox, and WebKit.

Playwright Features

  • Native SOCKS5 proxy support
  • Per-context proxy configuration
  • Chromium, Firefox, WebKit support
  • TypeScript and Python bindings

Configuration Examples

1. TypeScript - Basic Setup

import { chromium } from 'playwright';

const browser = await chromium.launch({
  proxy: {
    server: 'socks5://proxy.proxies.sx:10001',
    username: 'your_username',
    password: 'your_password'
  }
});

const page = await browser.newPage();
await page.goto('https://httpbin.org/ip');
console.log(await page.textContent('body'));
await browser.close();

2. TypeScript - Per-Context Proxy

import { chromium, Browser, BrowserContext } from 'playwright';

// Launch browser without proxy
const browser: Browser = await chromium.launch({ headless: true });

// Create contexts with different proxies
const usContext: BrowserContext = await browser.newContext({
  proxy: {
    server: 'socks5://proxy.proxies.sx:10001', // US port
    username: 'your_username',
    password: 'your_password'
  }
});

const ukContext: BrowserContext = await browser.newContext({
  proxy: {
    server: 'socks5://proxy.proxies.sx:10002', // UK port
    username: 'your_username',
    password: 'your_password'
  }
});

// Use different contexts for different regions
const usPage = await usContext.newPage();
const ukPage = await ukContext.newPage();

await usPage.goto('https://amazon.com');
await ukPage.goto('https://amazon.co.uk');

await browser.close();

3. Python - Async Playwright

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": "socks5://proxy.proxies.sx:10001",
                "username": "your_username",
                "password": "your_password"
            }
        )

        page = await browser.new_page()
        await page.goto("https://httpbin.org/ip")

        ip_info = await page.text_content("body")
        print(f"Current IP: {ip_info}")

        await browser.close()

asyncio.run(main())

4. Python - Sync API

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy={
            "server": "socks5://proxy.proxies.sx:10001",
            "username": "your_username",
            "password": "your_password"
        },
        headless=True
    )

    context = browser.new_context(
        viewport={"width": 1920, "height": 1080},
        user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64)..."
    )

    page = context.new_page()
    page.goto("https://example.com")
    page.screenshot(path="screenshot.png")

    browser.close()

5. HTTP Proxy (Alternative)

import { chromium } from 'playwright';

// HTTP proxy format
const browser = await chromium.launch({
  proxy: {
    server: 'http://proxy.proxies.sx:10001',
    username: 'your_username',
    password: 'your_password'
  }
});

// Or with inline credentials (not recommended for production)
const browserAlt = await chromium.launch({
  proxy: {
    server: 'http://user:pass@proxy.proxies.sx:10001'
  }
});

6. Production Stealth Setup

import { chromium } from 'playwright';

const browser = await chromium.launch({
  proxy: {
    server: 'socks5://proxy.proxies.sx:10001',
    username: 'your_username',
    password: 'your_password'
  },
  headless: true,
  args: [
    '--disable-blink-features=AutomationControlled',
    '--disable-features=IsolateOrigins,site-per-process'
  ]
});

const context = await browser.newContext({
  viewport: { width: 1920, height: 1080 },
  userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36',
  locale: 'en-US',
  timezoneId: 'America/New_York',
  geolocation: { longitude: -73.935242, latitude: 40.730610 },
  permissions: ['geolocation']
});

// Override navigator properties
await context.addInitScript(() => {
  Object.defineProperty(navigator, 'webdriver', { get: () => undefined });
});

const page = await context.newPage();
Pro Tip: Use browser contexts instead of new browsers when you need different proxy configurations. It's more efficient and faster.

Playwright Proxy Best Practices

Use Browser Contexts

Create new contexts for different proxy configurations instead of launching new browsers. Faster and more resource-efficient.

Match Timezone to Geo

Set timezoneId and geolocation to match your proxy location. Sites check for consistency between IP and browser settings.

Handle Network Errors

Implement retry logic with proxy rotation. If a page fails to load, switch to a different proxy port and retry.

Use SOCKS5 for Full Support

SOCKS5 proxies support all TCP traffic including WebSocket connections. HTTP proxies may not work with all web features.

Start Automating with Playwright

Get mobile proxies for reliable cross-browser automation.