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.

關於作者

SwiftProxy
Emily Chan
Swiftproxy首席撰稿人
Emily Chan是Swiftproxy的首席撰稿人,擁有十多年技術、數字基礎設施和戰略傳播的經驗。她常駐香港,結合區域洞察力和清晰實用的表達,幫助企業駕馭不斷變化的代理IP解決方案和數據驅動增長。
Swiftproxy部落格提供的內容僅供參考,不提供任何形式的保證。Swiftproxy不保證所含資訊的準確性、完整性或合法合規性,也不對部落格中引用的第三方網站內容承擔任何責任。讀者在進行任何網頁抓取或自動化資料蒐集活動之前,強烈建議諮詢合格的法律顧問,並仔細閱讀目標網站的服務條款。在某些情況下,可能需要明確授權或抓取許可。
Join SwiftProxy Discord community Chat with SwiftProxy support via WhatsApp Chat with SwiftProxy support via Telegram
Chat with SwiftProxy support via Email