0
0
Selenium-pythonDebug / FixBeginner · 4 min read

How to Handle Proxy in Selenium Python: Setup and Fixes

To handle a proxy in Selenium Python, use selenium.webdriver.ChromeOptions or selenium.webdriver.FirefoxOptions to set the proxy server before launching the browser. Configure the proxy by adding the --proxy-server=http://your.proxy:port argument to the options and then pass it to the WebDriver.
🔍

Why This Happens

When you try to use Selenium without setting a proxy properly, the browser may ignore your proxy settings or fail to connect through the proxy. This happens because Selenium WebDriver needs explicit instructions to route traffic via the proxy server.

python
from selenium import webdriver

proxy = "http://123.45.67.89:8080"

options = webdriver.ChromeOptions()
# Missing proxy setup here

driver = webdriver.Chrome(options=options)
driver.get("http://example.com")
Output
Browser opens but does not use the proxy; network requests go directly without proxy routing.
🔧

The Fix

To fix this, add the proxy server to the browser options before starting the WebDriver. This tells the browser to route all traffic through the specified proxy.

python
from selenium import webdriver

proxy = "123.45.67.89:8080"

options = webdriver.ChromeOptions()
options.add_argument(f"--proxy-server=http://{proxy}")

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

print("Page title:", driver.title)
driver.quit()
Output
Page title: Example Domain
🛡️

Prevention

Always configure proxy settings explicitly in your Selenium WebDriver options before launching the browser. Use environment variables or configuration files to manage proxy addresses for easier updates. Test your proxy setup with simple page loads to confirm it works before running complex tests.

  • Use ChromeOptions or FirefoxOptions to set proxies.
  • Validate proxy connectivity outside Selenium first.
  • Keep proxy credentials secure if authentication is needed.
⚠️

Related Errors

Common related errors include:

  • Timeouts: Proxy server not reachable causes connection timeouts.
  • Authentication errors: Proxy requires login but credentials are missing.
  • Invalid proxy format: Wrong proxy string format causes browser launch failures.

Quick fixes: verify proxy address, add authentication if needed, and use correct proxy syntax in options.

Key Takeaways

Always set proxy using browser options before starting Selenium WebDriver.
Use the correct proxy format: '--proxy-server=http://ip:port'.
Test proxy connectivity separately before Selenium tests.
Handle proxy authentication if your proxy requires it.
Keep proxy settings configurable for easy updates.