Playwright Proxy Setup: HTTP, SOCKS5, and Rotation

Set up a Playwright proxy in Node.js or Python with HTTP/SOCKS5, authentication, per-context routing, rotation, and practical fixes for browser automation.
Illustration of a 4G dongle connected to a Raspberry Pi and routed through a cloud proxy to a globe.

A playwright proxy is configured when you launch the browser or create a browser context, not after a page is already running. This guide shows the working pattern in Node.js and Python, including authenticated HTTP proxies, SOCKS5, one context per dongle, and controlled IP rotation.

What you need

Before changing your Playwright code, collect one complete proxy record:

  • HOST — the proxy hostname or IP address
  • PORT — the HTTP or SOCKS5 port
  • USERNAME and PASSWORD — if you use credential authentication
  • The rotation link or API details, if your workflow changes the mobile IP between jobs

You also need a supported Playwright project and a browser binary. For Node.js, install Playwright with npm install playwright; for Python, use python -m pip install playwright. Then install Chromium with npx playwright install chromium or playwright install chromium.

ProxyKite turns your own Huawei E3372 HiLink dongle and SIM into an HTTP and SOCKS5 proxy endpoint. The software and cloud routing handle the public route, so the box does not need a static IP or an inbound port opened. The data plan remains yours; ProxyKite does not supply the SIM, dongle, IP address, or bandwidth.

For the proxy format, keep the host, port, username, and password separate in your application. The ProxyKite proxy format guide explains the same values in host:port:user:pass, user:pass@host:port, and CSV exports.

If the hardware is not ready yet, see Huawei E3372 Proxy: HiLink to 4G Proxy and the powered USB hub guide before you assign routes to workers.

Step 1: Choose browser-level or context-level proxy

Use a browser-level proxy when every page in the browser should use the same route. Use a context-level proxy when separate workers, accounts, or dongles need separate routes and isolated cookies.

RequirementCorrect Playwright boundaryWhy
One route for the whole processchromium.launch({ proxy }) or chromium.launch(proxy=...)Every context in that browser inherits the launch route unless it defines another one.
Several dongles in one processbrowser.newContext({ proxy }) or browser.new_context(proxy=...)Each context can map to a different endpoint and has separate browser state.
Change the route for a running pageClose the context and create another onePlaywright does not expose a page.setProxy() or context.setProxy() operation.

The official Playwright network guide documents both scopes. A page is inside a context, so putting several pages in one context does not give each page a different proxy.

Step 2: Configure an authenticated HTTP proxy

For browser automation, start with an HTTP proxy endpoint. Playwright can tunnel HTTPS target traffic through an HTTP proxy, and its proxy object accepts the credentials separately from the server address. Do not put USERNAME:PASSWORD@ in the server field.

Node.js

const { chromium } = require('playwright');

const proxy = {
  server: 'http://HOST:HTTP_PORT',
  username: process.env.PROXY_USERNAME,
  password: process.env.PROXY_PASSWORD,
};

(async () => {
  const browser = await chromium.launch({
    headless: true,
    proxy,
  });

  const page = await browser.newPage();
  await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
  console.log(await page.title());
  await browser.close();
})();

Python

import os
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(
        headless=True,
        proxy={
            "server": "http://HOST:HTTP_PORT",
            "username": os.environ["PROXY_USERNAME"],
            "password": os.environ["PROXY_PASSWORD"],
        },
    )

    page = browser.new_page()
    page.goto("https://example.com", wait_until="domcontentloaded")
    print(page.title())
    browser.close()

The browser should open the page and print its title. If the proxy rejects the credentials, the navigation will fail with an authentication or tunnel error. Keep secrets in environment variables or your secret store; do not commit them to a test file, a screenshot, or a trace.

You can also use Playwright Test’s use.proxy setting in playwright.config.ts when every test in a project should use the same route. For a pool of dongles, context-level configuration is more useful because one browser process can host multiple isolated contexts.

Step 3: Map one browser context to one dongle

The context is the useful unit for a mobile-proxy pool. Give each context one endpoint, its own cookies, and one logical task. This keeps a login flow and its browser state associated with one route.

Node.js: several contexts

const { chromium } = require('playwright');

const routes = [
  {
    server: 'http://DONGLE_1_HOST:DONGLE_1_PORT',
    username: process.env.DONGLE_1_USER,
    password: process.env.DONGLE_1_PASSWORD,
  },
  {
    server: 'http://DONGLE_2_HOST:DONGLE_2_PORT',
    username: process.env.DONGLE_2_USER,
    password: process.env.DONGLE_2_PASSWORD,
  },
];

(async () => {
  const browser = await chromium.launch({ headless: true });

  for (const route of routes) {
    const context = await browser.newContext({ proxy: route });
    const page = await context.newPage();
    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
    console.log(await page.title());
    await context.close();
  }

  await browser.close();
})();

Python: one context per task

import os
from playwright.sync_api import sync_playwright

routes = [
    {
        "server": "http://DONGLE_1_HOST:DONGLE_1_PORT",
        "username": os.environ["DONGLE_1_USER"],
        "password": os.environ["DONGLE_1_PASSWORD"],
    },
    {
        "server": "http://DONGLE_2_HOST:DONGLE_2_PORT",
        "username": os.environ["DONGLE_2_USER"],
        "password": os.environ["DONGLE_2_PASSWORD"],
    },
]

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    for route in routes:
        context = browser.new_context(proxy=route)
        page = context.new_page()
        page.goto("https://example.com", wait_until="domcontentloaded")
        print(page.title())
        context.close()
    browser.close()

In a real worker, replace the for loop with your queue and make the route part of the job assignment. Close the context in a finally block so cookies, pages, and connections do not stay attached to a finished task. If you have ten dongles, create ten route records; you do not need ten separate Playwright installations.

ProxyKite’s per-dongle licence is $9 per dongle per month, billed monthly by Paddle.com, with no per-GB charge. It is a fit when you own the hardware and SIMs but want the cloud routing, proxy ports, rotation controls, quotas, and operator panel managed in one place.

Step 4: Use SOCKS5 when the client needs it

Playwright supports a SOCKS5 server address. Change only the scheme and port in the proxy object:

const context = await browser.newContext({
  proxy: {
    server: 'socks5://HOST:SOCKS5_PORT',
  },
});

The equivalent Python form is:

context = browser.new_context(
    proxy={"server": "socks5://HOST:SOCKS5_PORT"}
)

There is an important authentication detail. Playwright’s official examples describe username and password for HTTP(S) proxy authentication, while the server field documents SOCKS support. If an authenticated SOCKS5 endpoint fails, use the HTTP endpoint for Playwright or use an IP allowlist on the SOCKS5 route if your proxy setup supports it. Do not assume that adding HTTP-style credentials to a SOCKS5 configuration will work in every browser engine.

For ProxyKite, each dongle exposes HTTP and SOCKS5 access. Choose the HTTP port when you want the clearest username/password path in Playwright; choose SOCKS5 when your environment specifically requires it and its authentication mode is compatible. The proxy authentication guide covers user/password and IP-whitelist choices.

Step 5: Rotate between tasks, not inside a login flow

Playwright does not choose a new mobile IP by itself. It sends browser traffic to the endpoint you configured. The endpoint may be static, or your proxy platform may change its upstream IP when you request rotation.

For a mobile workflow, use this sequence:

  1. Create a context for the dongle and complete one coherent task.
  2. Close the context so its pages and connections are finished.
  3. Invoke the dongle’s on-demand rotation link or the rotation API.
  4. Wait for the rotation request to report success according to the proxy platform.
  5. Create a fresh context with the same proxy endpoint and run the next task.

Keep a login, checkout, multi-page form, or other stateful flow in one context. Rotating halfway through can change the network identity while the same cookies and session are still active. For independent jobs, a new context after each completed task is a clean boundary.

Here is the structure in Node.js. The URL is a placeholder for the rotation link assigned to your dongle; store it as a secret and use the request method documented for your endpoint.

async function rotateAndCreateContext(browser, proxy, rotationUrl) {
  const response = await fetch(rotationUrl);
  if (!response.ok) {
    throw new Error(`Rotation request failed: ${response.status}`);
  }

  return browser.newContext({ proxy });
}

If you use ProxyKite, start with the rotation API documentation and How a 4G Rotating Proxy Changes IPs. It supports on-demand rotation and timer-based policies from 60 seconds to 7 days, with a sticky option. The exact route and credentials should come from your panel or API configuration, not from a hard-coded example.

Step 6: Verify the route before running a real job

Test the proxy outside Playwright first. This separates a bad endpoint or credential from a browser configuration problem.

For an authenticated HTTP proxy:

curl --proxy "http://HOST:HTTP_PORT" \
  --proxy-user "USERNAME:PASSWORD" \
  https://api.ipify.org

For a SOCKS5 route where your proxy supports the chosen authentication method:

curl --proxy "socks5h://HOST:SOCKS5_PORT" \
  --proxy-user "USERNAME:PASSWORD" \
  https://api.ipify.org

Then make the same check in Playwright:

const page = await context.newPage();
await page.goto('https://api.ipify.org', { waitUntil: 'domcontentloaded' });
console.log(await page.textContent('body'));

The output should be the exit address returned by the proxy route, not the address you would see when connecting directly. Treat that value as a connectivity check only. It is not a guarantee about how a target website will classify or respond to the address.

Troubleshooting Playwright proxy errors

407 Proxy Authentication Required

The proxy received the request but did not accept the credentials. Check that the username and password are passed as separate fields, that the endpoint is the HTTP port, and that shell or environment-variable quoting has not changed a special character. Test the same record with curl --proxy-user.

ERR_PROXY_CONNECTION_FAILED or a timeout

Check the host and port, then test from the same machine that runs Playwright. Confirm that the box hosting your dongles has outbound internet access and that the dongle is online. If the endpoint is reachable with curl but not with Playwright, reduce the first navigation to https://example.com, use waitUntil: 'domcontentloaded', and inspect the browser and Playwright versions.

SOCKS5 works in curl but not in Playwright

Check the scheme exactly: socks5://, not an HTTP URL with a SOCKS5 port. Next, remove credentials from the Playwright object for a route that uses IP allowlisting, or switch to the authenticated HTTP endpoint. Playwright’s proxy credentials are not a universal SOCKS5-authentication adapter.

The IP changes during a workflow

Look for a timer or on-demand rotation policy that is shorter than the task. Use a sticky option where available, keep the whole workflow in one context, and only request a new rotation after the context is closed. If the task needs a stable route for days, review the timer and session design rather than trying to repair cookies after an IP change.

Several pages use the same dongle unexpectedly

Check where the proxy is configured. Pages in one browser context share that context’s route. Create separate contexts with separate proxy objects, and do not call browser.new_page() for jobs that need distinct routes without first creating the intended context.

A safe production pattern

For a small Playwright proxy pool, keep three pieces of data together: a route record, a task record, and a context lifetime. A route record contains the endpoint and secret references; a task record says what the browser must do; the context begins when the task starts and ends when it finishes.

Use a browser-level proxy for a single-route test suite. For multiple Huawei E3372 HiLink dongles on a Raspberry Pi or Linux box, launch one browser and assign one proxy context per dongle. Add bandwidth limits, website allow/block rules, and alerts at the proxy layer so an automation bug cannot quietly consume the entire SIM data plan.

If you are building beyond a few dongles, ProxyKite also includes sub-users, white-label controls, REST API access, webhooks, Telegram alerts, and exports. Fleets of 50 or more dongles can request volume pricing through ProxyKite contact. The getting started guide covers the box and dongle setup before you connect Playwright.

FAQ

Can Playwright use a proxy with username and password?

Yes. Pass the proxy server, username, and password as separate fields in the browser launch or browser-context configuration. The documented credential path is clearest for HTTP(S) proxies; keep the values in environment variables rather than source control.

Can I use a different proxy for each Playwright page?

Not directly. Proxy configuration belongs to the browser or browser context, so create one context per route and put the page inside the correct context. Multiple pages created in the same context share that context’s proxy.

Does Playwright rotate a proxy automatically?

No. Playwright sends traffic through the configured endpoint. To rotate a mobile IP, call the proxy platform’s rotation mechanism, close the old context, and create a new context for the next task.

Does Playwright support SOCKS5 proxies?

Yes, use a socks5://HOST:PORT server value. Test authentication separately because Playwright’s documented username/password fields target HTTP(S) proxy authentication; an authenticated HTTP endpoint or IP allowlist may be the more reliable choice for a SOCKS5-capable setup.

How much does ProxyKite cost for Playwright?

The ProxyKite per-dongle licence is $9 per dongle per month, with no per-GB charge. It is software and cloud routing for hardware and SIMs you own; taxes are added at checkout, and the service is billed monthly by Paddle.com.

Previous Article

Puppeteer Proxy Authentication: HTTP and SOCKS5 Guide

Next Article

Mobile Proxy vs VPN: Which Fits Your Workflow?