On this page
Playwright has proxy support built in, including the username and password, so authentication needs no extra step. You can set one proxy for the whole browser or a different proxy for each browser context.
Node.js
npm install playwright
npx playwright install chromium
import { chromium } from 'playwright';
const browser = await chromium.launch({
proxy: {
server: 'http://HOST:PORT',
username: 'USERNAME',
password: 'PASSWORD',
},
});
const page = await browser.newPage();
await page.goto('https://api.ipify.org?format=json');
console.log(await page.textContent('body'));
await browser.close();
Save the file as .mjs, or set "type": "module" in package.json, to use import and top-level await.
One proxy per context
import { chromium } from 'playwright';
const browser = await chromium.launch();
const context = await browser.newContext({
proxy: {
server: 'http://HOST:PORT',
username: 'USERNAME',
password: 'PASSWORD',
},
});
const page = await context.newPage();
await page.goto('https://api.ipify.org?format=json');
console.log(await page.textContent('body'));
await browser.close();
Contexts don't share cookies or storage. Give each one its own sticky line from the generator, and every context behaves like a separate visitor with its own IP.
Playwright Test
In the test runner, set the proxy in playwright.config.ts:
import { defineConfig } from '@playwright/test';
export default defineConfig({
use: {
proxy: {
server: 'http://HOST:PORT',
username: process.env.PROXY_USERNAME,
password: process.env.PROXY_PASSWORD,
},
},
});
Reading the credentials from environment variables keeps them out of your repository.
Python
pip install playwright
playwright install chromium
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
"server": "http://HOST:PORT",
"username": "USERNAME",
"password": "PASSWORD",
})
page = browser.new_page()
page.goto("https://api.ipify.org?format=json")
print(page.text_content("body"))
browser.close()
The async API works the same way, and new_context() takes the same proxy argument:
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://HOST:PORT",
"username": "USERNAME",
"password": "PASSWORD",
})
page = await context.new_page()
await page.goto("https://api.ipify.org?format=json")
print(await page.text_content("body"))
await browser.close()
asyncio.run(main())
Options worth knowing
bypass: a comma-separated list of hosts that skip the proxy, for example"localhost, .internal.example".- SOCKS5:
serveracceptssocks5://HOST:PORT, but theusernameandpasswordfields are for HTTP proxies. Since FreshProxies proxies use authentication, use the HTTP endpoint. - Firefox and WebKit take the same
proxyoption as Chromium.
Save traffic by blocking heavy files
Residential and mobile traffic is billed per GB. If you only need the page content, block images, media and fonts:
await page.route('**/*', (route) => {
const type = route.request().resourceType();
return ['image', 'media', 'font'].includes(type) ? route.abort() : route.continue();
});
In Python, the same idea uses page.route("**/*", handler) with route.abort() and route.continue_().
Match the location
If you target a country, set the browser's language and time zone to match, with the locale and timezoneId options of newContext() (locale and timezone_id in Python). Country targeting shows an example. For long sessions, use sticky lines; rotating vs sticky sessions explains the difference. If a page doesn't load through the proxy, start with troubleshooting.