Playwright Python Proxy Setup for Rotating IPs

You can configure Playwright Python to use rotating proxies at the browser or browser context level. This guide shows you how to set up HTTP and SOCKS5 connections, manage sticky sessions, and verify your exit IP.

Key points
  • Set proxy globally for a browser or per context using the `proxy` argument.
  • Use port 7070 for HTTP/HTTPS or port 7071 for SOCKS5 authentication.
  • Target countries, US states, and cities by adding options to the proxy username.
  • Maintain the same IP for minutes or hours using session IDs (sid) and TTL.

Playwright Python Proxy: Where to Configure

Playwright for Python allows you to set a proxy server when launching a browser or when creating a new browser context. The most common and flexible approach is to set the proxy at the browser context level, as this lets you run multiple isolated sessions with different proxy configurations simultaneously. You pass proxy settings as a dictionary to the proxy argument in either browser.new_context() or browser_type.launch(). This setup works for all three browser engines: Chromium, Firefox, and WebKit.

HTTP and HTTPS Proxy on Port 7070

To use the HTTP proxy gateway, configure Playwright to connect to gw.rotatingproxy.cc on port 7070. You must provide the server, port, username, and password in the proxy dictionary. The username includes your proxy user login and any targeting or session options, joined by underscores. Here is the basic setup for a rotating residential proxy in the United States:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context(
        proxy={
            "server": "http://gw.rotatingproxy.cc:7070",
            "username": "USERNAME_net.res_cc.us",
            "password": "PASSWORD"
        }
    )
    page = context.new_page()
    # Your automation code here
    page.goto("https://example.com")
    browser.close()

For HTTPS requests (which use the CONNECT method), the same server URL with the http:// scheme is correct. Playwright handles the protocol upgrade internally. This configuration is ideal for general web automation tasks like price monitoring or rank tracking.

SOCKS5 Proxy on Port 7071

If your toolchain or target site works better with SOCKS5, use port 7071. Playwright supports SOCKS5 proxy authentication directly. The setup is nearly identical, just change the port in the server string. Note that the SOCKS5 protocol handles both TCP connections and DNS resolution through the proxy, which can help avoid DNS leaks.

context = browser.new_context(
    proxy={
        "server": "socks5://gw.rotatingproxy.cc:7071",
        "username": "USERNAME_net.mob_cc.de",
        "password": "PASSWORD"
    }
)

Not all network configurations or legacy systems support SOCKS5 universally, but for modern browser automation, it's a reliable choice. Compare the technical details in our guide on HTTP vs SOCKS5 proxies.

Passing Geo-Targeting and Session Options

All proxy configuration, network type, country, state, city, and session, is encoded in the username string, following a specific grammar. Your base login is joined with options like key.value. For example, to target mobile IPs in Germany, Berlin, with a sticky session lasting 30 minutes, construct the username like this:

username = "USERNAME_net.mob_cc.de_city.berlin_sid.a7k2m9_ttl.30"

The sid (session ID) can be any 6 to 16 character string of lowercase letters or digits. Reusing the same sid within the ttl window will keep the same IP address. If you omit the sid, each new browser context or connection will receive a new IP from the chosen location. This is perfect for tasks like localization testing where you need consistent IPs from a specific city.

Verifying the Exit IP and Location

A simple method is to navigate to a service that returns your IP address within your Playwright script. You can parse the response to confirm.

page.goto("https://api.ipify.org")
ip = page.text_content("body")
print(f"Exit IP: {ip}")

# For more details like location
page.goto("https://ipinfo.io/json")
data = page.text_content("pre")
print(data)

If the IP does not match your targeting (e.g., you requested France but see a German IP), first check your username string for typos. Also, remember that IP geolocation databases are not perfect; our guide on geo IP accuracy explains the nuances. For a list of checking tools, see checking the exit IP and location.

Fixing Common Proxy Errors in Playwright

Playwright may throw errors or pages may fail to load when the proxy is misconfigured. Here are the common issues and their solutions.

Error: "Playwright timeout 30000ms exceeded" or general timeouts: This often indicates the proxy cannot establish a connection. Check your network allows outbound traffic to ports 7070 and 7071. Ensure your account has a positive balance and the proxy user is not paused. Increase the timeout in Playwright settings if needed, but first diagnose the network.

HTTP 407 Proxy Authentication Required: This means the username or password was rejected. Verify the username string is correctly formatted with no extra spaces. Confirm the proxy user's password and that the user is allowed to use the network specified (e.g., net.mob). Our error 407 guide has more steps.

TLS/SSL errors: If you see certificate warnings or failures, ensure you are using the correct port. Port 7070 supports HTTPS CONNECT tunneling. Sometimes, target sites may block known proxy IPs; trying a different network (like switching from datacenter to residential) can help. For advanced TLS handling, refer to TLS through a proxy.

Running Playwright at Scale with Proxies

When automating many pages or tasks concurrently, you need to manage proxies efficiently to avoid bans and control costs. Use separate browser contexts for each independent session or IP. This isolates cookies and cache, which is essential for social media management or managing multiple accounts.

# Example: Two concurrent contexts with different session IDs
context1 = browser.new_context(
    proxy={"server": "http://gw.rotatingproxy.cc:7070",
           "username": "USERNAME_net.res_cc.us_sid.abc123",
           "password": "PASSWORD"}
)
context2 = browser.new_context(
    proxy={"server": "http://gw.rotatingproxy.cc:7070",
           "username": "USERNAME_net.res_cc.us_sid.def456",
           "password": "PASSWORD"}
)

Respect the target site's robots.txt and implement polite crawling practices. Add delays between requests and rotate user agents if necessary. Be aware of your account's concurrency limit of 1,000 simultaneous connections. For large-scale projects, monitor your traffic usage to estimate costs, as shown in our bandwidth estimation guide.

Browser-Level vs Context-Level Proxy Settings

You can set the proxy at browser launch, which applies it to all contexts created from that browser unless overridden. This is useful when you want a default proxy for all traffic.

browser = p.chromium.launch(
    proxy={
        "server": "http://gw.rotatingproxy.cc:7070",
        "username": "USERNAME_net.dc_cc.jp",
        "password": "PASSWORD"
    }
)

However, context-level proxy settings are more powerful for multi-IP workflows. You can even create contexts without a proxy by passing proxy=None, which is helpful for mixing proxied and direct traffic. This granular control is key for app testing from different regions.

Integrating with Async Playwright and Pytest

The async API uses the same proxy dictionary structure. Here's how you set it up with 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()
        context = await browser.new_context(
            proxy={
                "server": "http://gw.rotatingproxy.cc:7070",
                "username": "USERNAME_net.isp_cc.gb",
                "password": "PASSWORD"
            }
        )
        page = await context.new_page()
        await page.goto("https://example.com")
        await browser.close()

asyncio.run(main())

When using Playwright with Pytest, you can define a fixture that provides a browser context with your proxy settings. This keeps your test code clean and makes it easy to run suites from different locations, beneficial for website uptime and change monitoring.

Advanced: Proxy for WebSocket and Other Protocols

Playwright's proxy configuration primarily affects HTTP, HTTPS, and SOCKS5 traffic for page navigation and resource loading. WebSocket connections initiated by the browser may also be routed through the same proxy tunnel, but this depends on the browser engine and proxy protocol. For critical WebSocket functionality, test thoroughly. If you need to proxy non-browser traffic from your Python script (like API calls), use a dedicated HTTP client like requests with our Python requests proxy integration.

Optimizing Performance and Reducing Traffic

Browser automation can consume significant proxy bandwidth. To reduce traffic and cost, block unnecessary resources like images, stylesheets, or fonts using context.route(). You can also enable request interception and abort requests for ads or tracking pixels.

context = await browser.new_context(proxy=...)
await context.route("**/*.{png,jpg,jpeg,svg,gif,woff,woff2}", lambda route: route.abort())
page = await context.new_page()

Cache responses locally when possible and compress data. These practices are part of how to reduce proxy traffic costs. For high-volume projects like AI training data collection, every saved megabyte adds up.

Next Steps and Related Integrations

Now that your Playwright Python proxy is working, explore other integrations for your data pipeline. For simpler HTTP requests, consider httpx proxy or Scrapy proxy. If you are managing multiple browser profiles, tools like Multilogin proxy setup can be combined with our service. Always refer to the official Playwright documentation for the latest API changes, as library details may evolve.

Questions

Can I use a different proxy for each tab in Playwright?

No, proxy settings are applied at the browser context level, not per tab. All pages within a single context share the same proxy connection. To use multiple proxies, create separate browser contexts.

Does Playwright support proxy auto-configuration (PAC) files?

No, Playwright's Python API does not support PAC files directly. You must specify the proxy server, port, and credentials explicitly in the proxy dictionary as shown in this guide.

How do I handle sites that show a CAPTCHA when using a proxy?

CAPTCHAs often appear due to IP reputation or behavior patterns. Using residential or mobile IPs can reduce their frequency. Implement longer delays between requests, mimic human interaction, and consider why CAPTCHAs appear for mitigation strategies.

Can I use these proxies with Playwright for testing a site behind a corporate firewall?

Yes, if the corporate firewall allows outbound connections to our gateway (ports 7070/7071). You may need to configure the proxy within Playwright to route traffic externally, which then exits from our IPs, potentially accessing geo-restricted internal test environments.

My Playwright script works without proxy but times out with proxy. What should I check?

First, test the proxy connection with a simple tool like curl using the same credentials. Verify your account balance is not zero. Check for network firewall rules blocking ports 7070 or 7071. Also, try reducing the Playwright timeout temporarily to get a faster failure for diagnosis.

Is it possible to rotate IPs on every page request within the same browser context?

Not directly within the same context. The proxy connection is established when the context is created. To get a new IP, you must create a new browser context (without a session ID) or close and reopen the context. For request-level rotation, consider an HTTP client instead of a browser.

Start with one top-up, use any network

Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.