Back to Blog
playwrightpythonweb-scrapingproxiesautomation

Playwright Proxy Setup with Authentication in Python

Modern proxy authentication setup for Playwright automation. Learn how to configure HTTP proxies with credentials for reliable web scraping and testing.

Published on May 13, 2025
5 min read
Playwright Proxy Setup with Authentication in Python

Using Authenticated HTTP Proxies in Playwright

Playwright offers first‑class support for HTTP proxies with authentication out of the box. In this guide, you’ll learn two proven methods—synchronous and asynchronous Python APIs—powered by a singlelogin:password@host:port string in config.py.

Prerequisites

  • Python 3.7+
  • pip install playwright and playwright install
  • Chromium, Firefox, or WebKit browser engines

Step 1: Centralize Proxy Credentials

Keep your proxy definition in one place. Create config.py:

config.py
# config.py
# Single line: login:password@host:port
PROXY = "pzAQ34:[email protected]:10878"

Method 1: Synchronous API (sync_playwright)

Use sync_playwright for straightforward scripts. Pass proxy when creating a context:

playwright_basic_proxy.py
from playwright.sync_api import sync_playwright
from config import PROXY

def main():
    user_pass, host_port = PROXY.split("@", 1)
    username, password = user_pass.split(":", 1)
    server = f"http://{host_port}"

    with sync_playwright() as pw:
        browser = pw.chromium.launch(headless=False)
        context = browser.new_context(
            proxy={
                "server": server,
                "username": username,
                "password": password
            }
        )
        page = context.new_page()
        page.goto("https://httpbin.org/ip")
        print(page.content())  # Should show proxy IP JSON
        page.screenshot(path="sync_proxy.png")
        context.close()
        browser.close()

if __name__ == "__main__":
    main()

Method 2: Asynchronous API (async_playwright)

For advanced workflows or high concurrency, use async_playwright:

playwright_async_proxy.py
import asyncio
from playwright.async_api import async_playwright
from config import PROXY

async def main():
    user_pass, host_port = PROXY.split("@", 1)
    username, password = user_pass.split(":", 1)
    server = f"http://{host_port}"

    async with async_playwright() as pw:
        browser = await pw.firefox.launch(headless=False)
        context = await browser.new_context(
            proxy={
                "server": server,
                "username": username,
                "password": password
            }
        )
        page = await context.new_page()
        await page.goto("https://httpbin.org/ip")
        content = await page.content()
        print(content)  # Proxy IP JSON
        await page.screenshot(path="async_proxy.png")
        await context.close()
        await browser.close()

if __name__ == "__main__":
    asyncio.run(main())

Best Practices & Tips

  • Rotate proxies by instantiating separate contexts for each proxy.
  • Use headless mode (headless=True) for CI/CD pipelines.
  • Set timeouts via page.set_default_timeout() to handle slow proxies.
  • Close contexts and browsers to free resources.

Conclusion

With Playwright’s native proxy option, authenticated HTTP proxies become first‑class citizens in your test and scraping workflows. Centralize your credentials in config.py, choose between sync or async APIs, and you’re ready to roll.

Need High-Quality Proxies for Your Projects?

HyperProxy offers premium IPv6 datacenter proxies from 60+ global locations at just $0.60 per proxy per month. Get instant delivery via Telegram with worldwide coverage including US, Europe, Asia-Pacific, and more.

Get Started with HyperProxy

Share this article

Related Articles

How to Use Proxies with Authentication in Selenium Python
seleniumpython

How to Use Proxies with Authentication in Selenium Python

Complete guide to configuring Selenium with authenticated proxies using Selenium Wire and Chrome extensions. Learn proxy rotation, error handling, and best practices for web scraping.

May 10, 2025Read More
Pyppeteer Proxy Authentication: Complete Python Guide
pyppeteerpython

Pyppeteer Proxy Authentication: Complete Python Guide

Step-by-step tutorial for setting up authenticated proxies in Pyppeteer. Includes code examples, troubleshooting tips, and performance optimization techniques.

April 23, 2025Read More
aiohttp Proxy Authentication: Async Python HTTP Requests
aiohttppython

aiohttp Proxy Authentication: Async Python HTTP Requests

Configure authenticated proxies with aiohttp for high-performance async HTTP requests. Includes session management, error handling, and connection pooling.

May 2, 2025Read More