How to Use Proxies in Playwright
Playwright accepts a proxy object with server, username and password at launch or per browser context, in Python, Node.js, Java and .NET.
Steps
- Pass proxy={server, username, password} to launch or new_context
- Use one context per session id to run several sticky IPs
- Route and abort images, fonts and media
- Check the exit IP once before the real job
Example code
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(proxy={
"server": "http://gw.gbproxies.cc:10000",
"username": "USERNAME-cc-jp",
"password": "PASSWORD",
})
page = browser.new_page()
page.route("**/*.{png,jpg,jpeg,webp,gif,woff2,mp4}", lambda r: r.abort())
page.goto("https://api.ipify.org")
print(page.inner_text("body"))
browser.close()Replace USERNAME and PASSWORD with the proxy login from the Proxies page of the console. Port 10000 is residential HTTP; see the ports of every product below.
Tips
- Per context proxies let one browser run many countries at once
- Use the SOCKS5 port only if your tool needs SOCKS; HTTP is simpler
Common questions
Can each tab use a different country?
Yes, create a browser context per country with its own username.