v1.10.82-f67ee7d
Skip to main content
← Back to Code Snippets

Python Selenium Proxy

Complete Selenium proxy integration example for Python with Hex Proxies. Includes authentication, timeouts, and error handling.

PythonSelenium
Install:pip install selenium webdriver-manager
Python / Selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

PROXY = "gate.hexproxies.com:8080"

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

service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)

try:
    driver.get("https://httpbin.org/ip")
    print("Page content:", driver.page_source[:200])
finally:
    driver.quit()

Why Selenium for Proxy Work

Selenium occupies a distinct niche in the proxy tooling landscape: it is the only widely-adopted framework that executes JavaScript, renders full page layouts, and interacts with dynamic content exactly as a real browser does. This matters for proxy work because an increasing number of websites rely on client-side rendering, JavaScript-based anti-bot challenges, and fingerprinting techniques that reject non-browser HTTP clients entirely. When your target site requires a real browser environment, routing Selenium traffic through gate.hexproxies.com:8080 gives you both the authenticity of browser rendering and the IP diversity of residential or datacenter proxies.

Selenium's WebDriver protocol supports Chrome, Firefox, Edge, and Safari, making it the most browser-diverse automation framework available. This cross-browser capability is valuable for ad verification, QA testing through proxies, and validating that geo-restricted content renders correctly across browser engines. The Python bindings are the most popular, but Selenium's proxy patterns translate directly to Java, C#, JavaScript, and Ruby bindings.

Configuration Patterns

Selenium passes proxy configuration through browser launch arguments rather than at the HTTP client level. The `--proxy-server` Chrome flag routes all browser traffic through the specified proxy. This approach handles HTTP, HTTPS, and WebSocket connections automatically, but it does not natively support proxy authentication. For authenticated proxies, you have three options: use Selenium Wire (a drop-in replacement that intercepts requests), load a Chrome extension that injects Proxy-Authorization headers, or use IP allowlisting in your Hex Proxies dashboard to authenticate by source IP.

For Firefox, configure the proxy through the `FirefoxProfile` or `Options` preferences: set `network.proxy.type` to 1 (manual), then specify `network.proxy.http` and `network.proxy.http_port`. Firefox handles proxy auth natively through a prompt, which you can automate using an extension or by pre-configuring credentials in the profile.

Common Pitfalls

The most dangerous mistake with Selenium proxy setups is leaking your real IP through WebRTC. Even when all HTTP traffic routes through the proxy, WebRTC STUN/TURN requests can bypass the proxy and reveal your actual IP address. Disable WebRTC by adding `--disable-webrtc` to Chrome arguments or by setting the `media.peerconnection.enabled` preference to false in Firefox. Also disable `--disable-features=WebRtcHideLocalIpsWithMdns` for complete protection.

Orphaned browser processes are another persistent issue. If your script crashes before calling `driver.quit()`, Chromium processes accumulate and consume memory until the system becomes unresponsive. Always wrap Selenium code in try/finally blocks, and consider using `atexit.register(driver.quit)` as a safety net. In production, run a periodic cleanup script that kills stale browser processes older than your maximum expected session duration.

Performance Optimization

Browser automation through proxies is inherently slower than raw HTTP requests because you pay the cost of rendering CSS, executing JavaScript, and loading images. Reduce this overhead by running Chrome in headless mode (`--headless=new`), disabling image loading (`--blink-settings=imagesEnabled=false`), and blocking unnecessary resource types through Chrome DevTools Protocol. These optimizations can reduce page load times from 5-10 seconds to under 2 seconds.

Limit parallel browser instances to match your system resources. Each Chrome instance consumes 100-300MB of RAM, so a machine with 8GB can realistically run 15-25 concurrent proxied browsers. Use a browser pool pattern where you pre-launch a set of browsers, assign them to tasks, and recycle them rather than launching and closing browsers for each request. This avoids the 2-3 second startup cost per browser instance.

Tips

  • 1
    Use webdriver-manager to keep ChromeDriver in sync with your browser version.
  • 2
    For authenticated proxies, use a Selenium Wire extension or browser profile with saved credentials.
  • 3
    Always call driver.quit() in a finally block to prevent orphaned browser processes.

Ready to Integrate?

Get proxy credentials and start coding in minutes.