Configuring and Using Proxies in Selenium
Automation is powerful—but only when it’s reliable. Hit too many IP blocks while scraping? Bounced by rate limits? If your Selenium scripts fail mid-run, it’s usually because they’re blind to network conditions. Enter proxies. They’re the secret weapon for advanced browser automation. With the right setup, proxies let you switch IPs, bypass restrictions, test globally, and run multiple sessions without conflicts.
Let’s dive in and set up a proxy in Selenium like a pro—Python examples included.
Why Proxies Are Needed for Selenium
Proxies are important. They allow you to:
- Change your IP address dynamically.
- Bypass rate limits and avoid automation roadblocks.
- Access geo-restricted content for testing or scraping.
- Separate sessions to prevent overlap or session conflicts.
- Improve anonymity and simulate real users from different regions.
Think of proxies as your traffic manager. They let you run scripts without alerting websites, masking your IP, and mimicking global user behavior.
How Selenium Uses Proxies
Selenium doesn’t manage proxies directly. Instead, you configure them through browser-specific options or capabilities. Chrome, Firefox, and Edge each handle proxies slightly differently—but the concept is the same: all traffic routes through your proxy server.
You’ll need:
- Selenium WebDriver
- Browser driver (ChromeDriver, GeckoDriver, etc.)
- Proxy host and port
- Optional: username and password for authenticated proxies
Once set up, your automated browser behaves like a user behind a proxy, giving you control and flexibility.
How to Set Up a Proxy in Chrome
For a basic HTTP or HTTPS proxy (no authentication), the setup is simple:
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? Add:
chrome_options.add_argument("--headless")
All traffic now flows through your proxy. Clean. Efficient. Reliable.
How to Handle Authenticated Proxies
Chrome doesn’t natively support username/password proxies via flags. Workaround: a tiny dynamically generated Chrome extension:
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 it in Selenium:
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 can also handle authenticated proxies and inspect network traffic in real time.
How to Set Up Firefox Proxy
Firefox allows profile-based proxies, making it more straightforward:
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")
Each session can have a unique proxy—perfect for authenticated proxies or isolated tests.
How to Use SOCKS Proxies
For higher anonymity or advanced scraping:
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")
Combine SOCKS proxies with headless mode for stealthy automation.
Managing and Rotating Proxies
Single proxies get burned fast. Rotate automatically:
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()
Proxy rotation keeps your automation under the radar, avoids bans, and mimics real-world traffic patterns.
Validating a Proxy
Always check:
driver.get("https://httpbin.org/ip")
The returned IP should match your proxy. Confirming early saves headaches later.
How to Troubleshoot Issues
- Proxy not applied? Check proxy type: http, https, socks4, or socks5.
- Authentication popups? Use the extension method.
- Connection errors? Verify credentials, firewall, or timeouts.
- HTTPS issues? Your proxy may only support HTTP or require CONNECT tunneling.
In Summary
Proxies transform Selenium automation from fragile to bulletproof. From testing and scraping to geo-targeting, they give you control, flexibility, and anonymity. Use authenticated proxies, rotate intelligently, and simulate real users. Chrome, Firefox, HTTP, HTTPS, or SOCKS—they’re all covered.
Set it up right, and your automation runs smoother, faster, and more reliably than ever.