How to Configure a Proxy in Selenium

When you’re running a Selenium script and your IP suddenly gets blocked mid-run, it’s enough to derail your entire workflow. That’s exactly where proxies come in. They give you control over how your automated browser interacts with the web—whether you need anonymity, geo-targeting, or multiple clean sessions. With the right proxy setup, your Selenium workflow becomes far more robust, stealthy, and flexible. Let’s cut through the noise and dive straight into setting up proxies in Selenium. We’ll cover Chrome and Firefox, handle authentication, explore SOCKS proxies, and even touch on proxy rotation for serious automation.

SwiftProxy
By - Emily Chan
2025-12-04 15:55:47

How to Configure a Proxy in Selenium

Why You Need a Proxy in Selenium

A proxy isn't just a shield—it's a tool. Here's what it can do for you:

Change your IP to avoid blocks.

Bypass rate limits for high-volume requests.

Access geo-restricted content seamlessly.

Separate multiple sessions without conflicts.

Enhance anonymity when scraping sensitive data.

If your Selenium automation gets blocked frequently, integrating proxies is necessary. It's the difference between smooth execution and hitting constant roadblocks.

How Selenium Works With Proxies

Selenium doesn't set proxies directly. Instead, it passes proxy settings to the browser via options or capabilities. Chrome, Firefox, and Edge all have slightly different methods, so knowing the nuances is crucial.

Requirements for proxy setup:

Selenium WebDriver

Browser driver (e.g., ChromeDriver)

Proxy server (host and port)

Optional: username/password for authenticated proxies

Once configured, Selenium will route all browser traffic through the specified proxy, making your automation safer and more realistic.

Configuring a Proxy in Chrome Using Python

For a simple HTTP/HTTPS proxy (no authentication):

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

proxy = "123.45.67.89:8080"

chrome_options = Options()
chrome_options.add_argument(f"--proxy-server=http://{proxy}")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")

Want to run headless? Just add:

chrome_options.add_argument("--headless")

Configuring an Authenticated Proxy

Chrome can't handle username/password proxies directly from command-line flags. Instead, you can generate a small Chrome extension dynamically:

import zipfile

def create_extension(proxy_host, proxy_port, username, password):
    manifest = """
    {
      "version": "1.0.0",
      "manifest_version": 2,
      "name": "ProxyAuth",
      "permissions": ["proxy", "tabs", "unlimitedStorage", "storage", "<all_urls>", "webRequest", "webRequestBlocking"],
      "background": {
        "scripts": ["background.js"]
      }
    }
    """

    background = f"""
    chrome.proxy.settings.set({ {
      value: { {
        mode: "fixed_servers",
        rules: { {
          singleProxy: { {
            scheme: "http",
            host: "{proxy_host}",
            port: parseInt({proxy_port})
          }}
        }}
      }},
      scope: "regular"
    }});

    chrome.webRequest.onAuthRequired.addListener(
      function handler(details) { {
        return { {
          authCredentials: { {username: "{username}", password: "{password}"}}
        }};
      }},
      { {urls: ["<all_urls>"]}},
      ["blocking"]
    );
    """

    with zipfile.ZipFile("proxy_auth.zip", "w") as zp:
        zp.writestr("manifest.json", manifest)
        zp.writestr("background.js", background)

Load the extension:

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

proxy_host = "123.45.67.89"
proxy_port = "8080"
username = "user123"
password = "pass123"

create_extension(proxy_host, proxy_port, username, password)

chrome_options = Options()
chrome_options.add_extension("proxy_auth.zip")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")

Selenium Wire is another way to handle proxy authentication and inspect traffic seamlessly.

Firefox Proxy Setup

Firefox makes proxy configuration more direct through profiles:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "123.45.67.89")
profile.set_preference("network.proxy.http_port", 8080)
profile.set_preference("network.proxy.ssl", "123.45.67.89")
profile.set_preference("network.proxy.ssl_port", 8080)
profile.update_preferences()

driver = webdriver.Firefox(firefox_profile=profile, options=Options())
driver.get("https://example.com")

Using SOCKS Proxies

Selenium supports SOCKS proxies, perfect for high-anonymity use cases:

proxy = "123.45.67.89:1080"
chrome_options = Options()
chrome_options.add_argument(f"--proxy-server=socks5://{proxy}")

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")

Headless mode + SOCKS proxies = fully automated, stealthy browsing.

Proxy Rotation and Control

Single proxies get blocked fast. Rotate them to stay ahead:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import random

proxies = [
    "123.45.67.89:8080",
    "98.76.54.32:3128",
    "11.22.33.44:8000"
]

def get_random_proxy():
    return random.choice(proxies)

for i in range(5):
    proxy = get_random_proxy()
    chrome_options = Options()
    chrome_options.add_argument(f"--proxy-server=http://{proxy}")
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("https://httpbin.org/ip")
    print(driver.page_source)
    driver.quit()

Rotate intelligently to bypass IP bans, geo-restrictions, and anti-scraping defenses.

Checking Your Proxy

Verify that Selenium is using your proxy:

driver.get("https://httpbin.org/ip")

Check the returned IP. It should match your proxy.

Tips for Fixing Issues

Proxy not applied: Ensure the proxy type matches (http, https, socks4, socks5).

Authentication popup: Use the custom extension approach for Chrome.

Connection errors: Double-check firewall, timeout, or credentials.

HTTPS issues: Some proxies only support HTTP; CONNECT tunneling may be required.

In Summary

Setting up a proxy in Selenium isn't complicated—but doing it right makes automation far more reliable. Whether you're testing, scraping, or geo-targeting, proxies let you simulate real users, manage sessions, and bypass blocks. Use Chrome or Firefox options, handle authentication smartly, rotate your proxies, and your Selenium scripts will run like clockwork.

Note sur l'auteur

SwiftProxy
Emily Chan
Rédactrice en chef chez Swiftproxy
Emily Chan est la rédactrice en chef chez Swiftproxy, avec plus de dix ans d'expérience dans la technologie, les infrastructures numériques et la communication stratégique. Basée à Hong Kong, elle combine une connaissance régionale approfondie avec une voix claire et pratique pour aider les entreprises à naviguer dans le monde en évolution des solutions proxy et de la croissance basée sur les données.
Le contenu fourni sur le blog Swiftproxy est destiné uniquement à des fins d'information et est présenté sans aucune garantie. Swiftproxy ne garantit pas l'exactitude, l'exhaustivité ou la conformité légale des informations contenues, ni n'assume de responsabilité pour le contenu des sites tiers référencés dans le blog. Avant d'engager toute activité de scraping web ou de collecte automatisée de données, il est fortement conseillé aux lecteurs de consulter un conseiller juridique qualifié et de revoir les conditions d'utilisation applicables du site cible. Dans certains cas, une autorisation explicite ou un permis de scraping peut être requis.
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email