How to Handle Proxy in Selenium Python: Setup and Fixes
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.
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")
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.
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()
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
ChromeOptionsorFirefoxOptionsto 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.