ProxyForge Download

Headless Browser Proxy Setup: Selenium, Playwright, and Residential Proxies

Headless Browser Proxy Setup: Selenium, Playwright, and Residential Proxies

Setting up proxies for headless browsers like Selenium and Playwright can be crucial for tasks such as web scraping, automation, and testing. This guide will walk you through setting up residential proxies from providers like Bright Data and SmartProxy using both Selenium and Playwright. We'll also compare the use of sticky vs rotating sessions.

Introduction

Using headless browsers with proxies is essential to avoid being detected by anti-bot systems. Proxies provide an additional layer of security and can help manage your IP address usage, especially when scraping or automating tasks at scale.

We will cover two popular Python libraries for browser automation: Selenium and Playwright. Both have their strengths and weaknesses, but they both support proxy configurations. We'll also discuss how to handle proxy authentication, which is a common requirement with residential proxies.

Setting Up Proxies with Playwright

Playwright is known for its simplicity and ease of use, particularly in handling proxies. Here's how you can set up a proxy with Playwright:

Step 1: Install Playwright

First, ensure you have Playwright installed. You can do this using pip:

pip install playwright

Then, install the browser binaries:

playwright install

Step 2: Launch Browser with Proxy

Here's how to launch a Chromium browser instance with a proxy that requires authentication:

from playwright.sync_api import sync_playwright

def run(playwright):
    browser = playwright.chromium.launch(headless=True, args=['--proxy-server=http://your-proxy-url:port'])
    context = browser.new_context(proxy={
        "server": "http://your-proxy-url:port",
        "username": "your-username",
        "password": "your-password"
    })
    page = context.new_page()
    page.goto('https://example.com')
    print(page.title())
    browser.close()

with sync_playwright() as playwright:
    run(playwright)

Note: The --proxy-server argument is used to specify the proxy server, and the context method allows you to set the username and password for authentication.

Setting Up Proxies with Selenium

Selenium requires a bit more setup when dealing with proxies due to its older architecture. Here's how to set up a proxy with Selenium:

Step 1: Install Selenium

First, ensure you have Selenium installed. You can do this using pip:

pip install selenium

Step 2: Launch Browser with Proxy

Selenium uses the ChromeOptions extension approach for setting up proxies with authentication. Here's an example of how to set it up:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--proxy-server=http://your-proxy-url:port")

# Create a proxy extension for authentication
manifest_json = """
{
    "version": "1.0.0",
    "name": "Chrome Proxy",
    "permissions": [
        "proxy",
        "tabs",
        "",
        "unlimitedStorage",
        "storage"
    ],
    "background": {
        "scripts": ["background.js"]
    }
}
"""

background_js = """
var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "%s",
        port: parseInt(%d)
      },
      bypassList: ["localhost"]
    }
};

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
""" % ("your-proxy-url", 8080)

pluginfile = 'proxy_auth_plugin.zip'

with zipfile.ZipFile(pluginfile, 'w') as zp:
    zp.writestr("manifest.json", manifest_json)
    zp.writestr("background.js", background_js)

chrome_options.add_argument("--load-extension=" + os.path.abspath(pluginfile))

service = Service('path/to/chromedriver')
driver = webdriver.Chrome(service=service, options=chrome_options)

try:
    driver.get('https://example.com')
    print(driver.title)
finally:
    driver.quit()

Note: This example uses a Chrome extension to handle proxy authentication. The background.js script sets up the proxy configuration.

Residential Proxies from Bright Data and SmartProxy

Residential proxies are IP addresses that come from real users, making them less likely to be detected by anti-bot systems. Providers like Bright Data and SmartProxy offer residential proxies with various configurations.

Sticky vs Rotating Sessions

Example Configuration

Here's how you might configure a rotating proxy session with Playwright:

from playwright.sync_api import sync_playwright
import random

proxies = [
    {"server": "http://proxy1.brightdata.com:port", "username": "user1", "password": "pass1"},
    {"server": "http://proxy2.brightdata.com:port", "username": "user2", "password": "pass2"}
]

def run(playwright):
    browser = playwright.chromium.launch(headless=True)
    for proxy in proxies:
        context = browser.new_context(proxy=proxy)
        page = context.new_page()
        page.goto('https://example.com')
        print(page.title())
        context.close()

with sync_playwright() as playwright:
    run(playwright)

In this example, we use a list of proxies and rotate through them for each session.

Conclusion

Setting up proxies with headless browsers like Selenium and Playwright is essential for effective web scraping and automation. While Playwright offers a simpler API for proxy authentication, Selenium requires more setup due to its older architecture. When using residential proxies from providers like Bright Data and SmartProxy, consider whether you need sticky or rotating sessions based on your use case.

If you'd rather not manage proxy lists manually, ProxyForge auto-tests and rotates proxies locally on your machine — one-off $29 unlock, no subscription: https://proxyforge.dev/#download.

Free proxies break. ProxyForge doesn't.

The free lists above are public and often unreliable. ProxyForge auto-tests and rotates proxies on your machine, your IP — no stale lists, no shared pools, no surprises. One-off purchase, no subscription.

Download ProxyForge free →