Automation & Development

How to Use Proxies with Playwright: Browser-Level and Context-Level Setup

Playwright supports proxies at browser launch and per browser context. This tutorial shows both patterns in Python and JavaScript, plus authentication, bypass rules and debugging tips.

ProxyGrove Editorial · · · 9 min read

Blue code editor illustration with a Playwright script configuring a proxy server option

TL;DR: Playwright accepts a proxy option with server, username, password and bypass. Set it in browserType.launch() for one proxy per browser, or in browser.newContext() for a different proxy per context. Verify the exit IP with a test page, keep credentials in environment variables and watch for 407 and timeout errors.

How Playwright handles proxies

Playwright controls Chromium, Firefox and WebKit through a single API, and proxy settings are part of that API. The proxy object has four fields that matter in everyday work: server, username, password and bypass. The official network documentation describes them, and the Python variant is documented in the Playwright for Python guide.

ProxyGrove maintains an Playwright integration page with setup notes for its endpoints. This article explains the general technique and uses placeholders for every credential.

Option one: proxy at browser launch

Passing a proxy to launch() routes every page in that browser through it. This is the simplest pattern when a whole job uses one exit.

// Node.js
const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch({
    proxy: {
      server: 'http://YOUR_PROXY_HOST:YOUR_PROXY_PORT',
      username: process.env.PROXY_USER,   // YOUR_PROXY_USERNAME
      password: process.env.PROXY_PASSWORD // YOUR_PROXY_PASSWORD
    }
  });
  const page = await browser.newPage();
  await page.goto('https://example.com/');
  console.log(await page.title());
  await browser.close();
})();

Option two: a different proxy per context

Contexts are isolated browser profiles that share one browser process. A proxy set on newContext() applies only to that context, so you can run several identities side by side without launching several browsers. This is the preferred pattern for multi-account or multi-region tests because contexts also isolate cookies and storage.

# Python
import os
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    context = browser.new_context(
        proxy={
            "server": "http://YOUR_PROXY_HOST:YOUR_PROXY_PORT",
            "username": os.environ["PROXY_USER"],
            "password": os.environ["PROXY_PASSWORD"],
        }
    )
    page = context.new_page()
    page.goto("https://example.com/")
    print(page.title())
    browser.close()

Chromium on some platforms requires a placeholder global proxy at launch time before per-context proxies take effect. If a context proxy appears to be ignored in your version, consult the current BrowserType.launch reference for the recommended workaround.

Proxy option reference

FieldPurposeNotes
serverProxy URL with scheme and portUse http:// for HTTP(S) proxies; socks5:// is accepted by Chromium
username / passwordProxy credentialsHandled by the browser's proxy auth challenge
bypassComma-separated hosts that skip the proxyUseful for localhost and internal services

Authenticated SOCKS5 proxies are not supported in every browser engine, so if you need SOCKS5 with credentials, test it in your exact browser and version, or use an HTTP endpoint instead.

Verify the exit IP

  1. Create a context with the proxy configured.
  2. Open an IP-echo page that you trust and read the visible address.
  3. Compare against your direct address to confirm the proxy is in use.
  4. Open a second context with different credentials or session parameters and confirm it reports a different address, if that is your design.
  5. Check that localhost or internal hosts listed in bypass still load directly.

Playwright's tracing and HAR recording are helpful for debugging. Record a trace around the first failing navigation and review the network tab.

Sessions, concurrency and cleanup

Create one context per logical identity and close it when the task ends. Contexts are cheap compared with browsers, but they still consume memory, so cap concurrency at a level your machine handles comfortably. If you use sticky sessions from your provider, generate the session identifier when you create the context and keep it for the context's lifetime so cookies and IP stay aligned. If you rotate per navigation, create fresh contexts instead of trying to change a proxy on an existing one; a context's proxy is fixed at creation.

For high-volume data collection read proxies for web scraping, and consider blocking images, fonts and media with page.route to reduce bandwidth on traffic-based plans.

Troubleshooting

  • ERR_PROXY_CONNECTION_FAILED: check host, port and whether your network allows outbound connections to the proxy port.
  • 407 or repeated login prompts: verify the username and password and that the account has remaining traffic or an active plan.
  • Timeouts: increase navigation timeouts modestly and test the same URL with curl to isolate browser problems from proxy problems.
  • TLS errors: do not disable verification as a default; investigate whether a middlebox or an incorrect system clock is responsible.

The article on common proxy connection problems expands on each of these causes.

Patterns for multi-identity test suites

Teams that use Playwright for localization or account-level testing often need several proxy identities in one run. A clean approach is a fixture that creates a context for a named identity, using a configuration map from identity name to proxy settings. Tests then request an identity by name and never see credentials. The fixture closes the context when the test ends, which keeps memory and open connections in check.

Keep tests deterministic where possible. Network variability introduces flakiness, so give navigation steps explicit timeouts, retry only the network-facing step and fail with a message that includes the identity name and the exit IP reported by a quick check. When a run fails in continuous integration, that single line often tells you whether the proxy, the site or the test is responsible.

Common mistakes

  • Putting credentials in the server URL in one place and in username/password fields in another.
  • Sharing a single context across tasks and mixing cookies from different identities.
  • Using a rotating endpoint for a login flow and getting logged out between pages.
  • Forgetting that WebRTC and other browser features can reveal network details; verify with a leak test page when privacy matters.
  • Printing full proxy URLs, including passwords, in logs.

Frequently asked questions

Can I set a different proxy for each page?

A proxy is applied at the browser or context level, so create a separate context for each identity that needs its own proxy.

How do I authenticate to a proxy in Playwright?

Provide username and password in the proxy option. Playwright answers the proxy's authentication challenge for you.

Does Playwright support SOCKS5?

Chromium accepts socks5:// servers, but authenticated SOCKS5 support varies by engine and version, so test before relying on it.

Why does my IP check still show my real address?

The proxy may not be applied to the context you are using, the page may be served from cache, or the test uses a service that reads a different header. Verify with a clean context.

Where can I find ProxyGrove-specific notes?

See the Playwright page in the ProxyGrove integrations directory for endpoint details.

Conclusion

For Playwright, think in contexts: one context per identity, one proxy per context, with credentials loaded from the environment and the exit IP verified before the real work begins. When the setup behaves, you can scale it with the confidence of repeatable tests. Continue with the ProxyGrove Playwright integration or compare tooling in the integrations directory.

References

About the author

ProxyGrove Editorial

ProxyGrove Editorial publishes practical guides on proxy technology, automation, and responsible network workflows for international teams.

Ready to Find Your Perfect Proxy Solution?

Explore ProxyGrove plans, then apply credentials in your tools with our Integrations guides.