Set a proxy when you launch the browser
The simplest setup sends every page in the browser through one proxy. Pass a proxy object to launch():
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(
proxy={
"server": "http://HOST:823",
"username": "USERNAME",
"password": "PASSWORD",
}
)
page = browser.new_page()
page.goto("https://api.ipify.org")
print(page.inner_text("body")) # the proxy's IP
browser.close()The same in Node.js:
import { chromium } from 'playwright';
const browser = await chromium.launch({
proxy: { server: 'http://HOST:823', username: 'USERNAME', password: 'PASSWORD' },
});
const page = await browser.newPage();
await page.goto('https://api.ipify.org');
console.log(await page.innerText('body'));
await browser.close();The server uses http:// even for HTTPS sites: the browser tunnels HTTPS through the proxy with the CONNECT method, so the pages stay encrypted end to end.
Use a different proxy per context
A browser context is an isolated session with its own cookies and storage. Giving each context its own proxy settings lets one browser check several locations at once:
with sync_playwright() as p:
browser = p.chromium.launch()
for country in ["us", "de", "jp"]:
context = browser.new_context(
proxy={
"server": "http://HOST:823",
"username": f"USERNAME__cr.{country}",
"password": "PASSWORD",
}
)
page = context.new_page()
page.goto("https://api.ipify.org")
print(country, page.inner_text("body"))
context.close()
browser.close()The country code in the username (__cr.de) picks where the IP comes from. Location targeting lists city, state and ZIP options too.
Keep one IP per page with sticky sessions
On a rotating gateway, every new connection gets a new IP. A browser opens several connections per page and reuses them, so one page load can mix IPs, which some sites treat as odd and which makes location checks inconsistent.
Give each context a session name instead. The gateway then keeps one IP for that session for about 30 minutes:
import uuid
session = uuid.uuid4().hex[:8]
context = browser.new_context(
proxy={
"server": "http://HOST:823",
"username": f"USERNAME__cr.de;sessid.{session}",
"password": "PASSWORD",
}
)Create a new context with a new session name whenever you want a new IP. Rotating proxies explains when to rotate and when to stay sticky.
Authentication and SOCKS5
Playwright accepts a username and password in the proxy object for HTTP proxies. Chromium does not support username and password authentication for SOCKS5 proxies, so an authenticated socks5:// server will fail. You have two options:
- Use the HTTP port (
http://HOST:823) with credentials. For websites, it works just as well. - Allowlist your server's public IPv4 address in the dashboard and use SOCKS5 (
socks5://HOST:824) without credentials.
See SOCKS5 vs HTTP proxy for the differences.
Block heavy assets to save traffic
Per-GB proxies charge for every byte the browser downloads. Images, fonts and video are usually most of a page's weight and rarely needed for data checks. Block them with a route:
BLOCK = {"image", "font", "media"}
page.route(
"**/*",
lambda route: route.abort()
if route.request.resource_type in BLOCK
else route.continue_(),
)A page that weighs several megabytes with everything loaded can drop to a few hundred kilobytes. Proxy pricing explained shows how much that saves.
Match locale and timezone to the proxy
For location checks, make the browser look like a local visitor so sites serve their localised version:
context = browser.new_context(
proxy={"server": "http://HOST:823", "username": "USERNAME__cr.de", "password": "PASSWORD"},
locale="de-DE",
timezone_id="Europe/Berlin",
)Without this, a German IP with an English browser locale may get an English or international page.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
net::ERR_PROXY_CONNECTION_FAILED | Wrong host or port, or outbound traffic blocked | Check the host and port in your dashboard; allow outbound connections |
407 or an authentication prompt | Wrong credentials | Check the username and password; special characters are fine in the proxy object |
| SOCKS5 fails with credentials | Chromium lacks SOCKS5 auth | Use the HTTP port, or allowlist your IP |
| Navigation timeouts | Slow route, distant country or heavy page | Raise timeout in goto(), block assets |
| The IP changes mid-page | Rotating port without a session | Add sessid to the username |
Use browser automation responsibly
Automate only public pages, follow each site's terms and robots.txt, and keep the pace close to a person's. Do not use Playwright with proxies to create accounts, log in to accounts you do not own, or get around logins, paywalls or other access controls. FuseProxy's acceptable use policy lists what is not allowed, and is web scraping legal? covers the legal side. For plain HTTP requests without a browser, see how to use a proxy with Python requests.