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
andplaywright install
- Chromium, Firefox, or WebKit browser engines
Step 1: Centralize Proxy Credentials
Keep your proxy definition in one place. Create 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:
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
:
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.