Skip to content
Guides 3 Connection Type: 1 views

Using Proxies in Python with Playwright

Configuring Proxy in Playwright for Python: HTTP and SOCKS5 proxies, authentication, browser contexts, and bypassing anti-bots.

Using Proxies in Python with Playwright

Playwright and Proxies

Playwright is a browser automation framework from Microsoft, supporting Chromium, Firefox, and WebKit. The Python version of Playwright has built-in proxy support at the browser level and for individual contexts.

Installation

pip install playwright
playwright install  # downloads browsers

Basic Proxy Configuration

HTTP Proxy

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy={
            "server": "http://proxy_ip:port"
        }
    )
    page = browser.new_page()
    page.goto("https://httpbin.org/ip")
    print(page.content())
    browser.close()

With Authentication

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        proxy={
            "server": "http://proxy_ip:port",
            "username": "user",
            "password": "pass"
        }
    )
    page = browser.new_page()
    page.goto("https://httpbin.org/ip")
    print(page.content())
    browser.close()

SOCKS5 Proxy

browser = p.chromium.launch(
    proxy={"server": "socks5://proxy_ip:port"}
)

Asynchronous Mode

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_ip:port",
                "username": "user",
                "password": "pass"
            }
        )
        page = await browser.new_page()
        await page.goto("https://httpbin.org/ip")
        content = await page.content()
        print(content)
        await browser.close()

asyncio.run(main())

Context-Level Proxies

Playwright allows you to set different proxies for different contexts (profiles) within a single browser:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()

    # Context 1 — US proxy
    context1 = browser.new_context(
        proxy={
            "server": "http://us_proxy:port",
            "username": "user",
            "password": "pass"
        }
    )

    # Context 2 — Germany proxy
    context2 = browser.new_context(
        proxy={
            "server": "http://de_proxy:port",
            "username": "user",
            "password": "pass"
        }
    )

    page1 = context1.new_page()
    page2 = context2.new_page()

    page1.goto("https://httpbin.org/ip")
    page2.goto("https://httpbin.org/ip")

    print("US:", page1.content())
    print("DE:", page2.content())

    browser.close()

This is a powerful feature: one browser, multiple contexts with different proxies and fingerprints.

Bypassing Proxies for Specific Domains

browser = p.chromium.launch(
    proxy={
        "server": "http://proxy_ip:port",
        "bypass": "localhost,*.local,example.com"
    }
)

Domains in the bypass list are accessed directly, bypassing the proxy.

Headless and Headed Modes

# Headless (without GUI) — for scraping
browser = p.chromium.launch(headless=True, proxy={"server": "http://proxy:port"})

# Headed (with GUI) — for debugging
browser = p.chromium.launch(headless=False, proxy={"server": "http://proxy:port"})

Working with Different Browsers

Playwright supports three engines:

# Chromium
browser = p.chromium.launch(proxy=proxy_config)

# Firefox
browser = p.firefox.launch(proxy=proxy_config)

# WebKit (Safari)
browser = p.webkit.launch(proxy=proxy_config)

All three support proxies identically.

Stealth Mode and Anti-bot Evasion

Playwright is detected by anti-bot systems. To bypass:

playwright-stealth

pip install playwright-stealth
from playwright.sync_api import sync_playwright
from playwright_stealth import stealth_sync

with sync_playwright() as p:
    browser = p.chromium.launch(proxy={"server": "http://proxy:port"})
    page = browser.new_page()
    stealth_sync(page)  # applies stealth patches
    page.goto("https://target-site.com")

Evasion Recommendations

  1. Use residential or mobile proxies
  2. Set a realistic User-Agent
  3. Configure viewport size
  4. Add delays between actions
  5. Simulate mouse movements and scrolling

Screenshots and PDFs via Proxy

page.goto("https://example.com")
page.screenshot(path="screenshot.png", full_page=True)
page.pdf(path="page.pdf")

Intercepting Requests

def handle_route(route):
    # Modifying requests
    headers = route.request.headers
    headers["X-Custom-Header"] = "value"
    route.continue_(headers=headers)

page.route("**/*", handle_route)
page.goto("https://example.com")

Practical Example: Scraping with Rotation

import asyncio
from playwright.async_api import async_playwright
import random

PROXIES = [
    {"server": "http://proxy1:port", "username": "u", "password": "p"},
    {"server": "http://proxy2:port", "username": "u", "password": "p"},
    {"server": "http://proxy3:port", "username": "u", "password": "p"},
]

async def scrape_page(playwright, url):
    proxy = random.choice(PROXIES)
    browser = await playwright.chromium.launch(proxy=proxy)
    page = await browser.new_page()
    try:
        await page.goto(url, timeout=30000)
        title = await page.title()
        return title
    except Exception as e:
        return f"Error: {e}"
    finally:
        await browser.close()

async def main():
    urls = [f"https://example.com/page/{i}" for i in range(10)]
    async with async_playwright() as p:
        tasks = [scrape_page(p, url) for url in urls]
        results = await asyncio.gather(*tasks)
        for url, result in zip(urls, results):
            print(f"{url}: {result}")

asyncio.run(main())

Conclusion

Playwright is a powerful tool for working with proxies in Python. Context-level proxy support allows running multiple sessions with different IPs within a single browser. Combined with stealth patches and residential proxies, Playwright is effective for scraping protected websites.

Auto-update: 06.03.2026
All Categories

Advantages of our proxies

25,000+ proxies from 120+ countries