Selenium Proxy with Authentication: Python Setup Guide
To use a Selenium proxy with authentication, you must route traffic through a local forwarder because Chrome's --proxy-server flag ignores credentials. This guide covers the pproxy method for Chrome and the Firefox profile alternative, plus how to control location and sessions.
- Chrome requires a local forwarder like pproxy to inject proxy credentials.
- Firefox can accept credentials directly via a profile and preferences.
- Target countries, states, and cities or use sticky sessions via the username string.
- Verify the exit IP with a simple JavaScript execution in the browser.
Where Selenium Proxy Settings Live
Selenium itself does not have built-in proxy settings; you configure the proxy through the browser's launch options. For Chrome, you use ChromeOptions to set a proxy server address. For Firefox, you use FirefoxOptions or a FirefoxProfile to set network preferences. The critical detail is that Chrome's native proxy setting does not support embedding a username and password, which is why a local forwarding proxy becomes necessary. This is a fundamental constraint of the Chrome/Chromium browser engine, not a limitation of Selenium or the proxy service.
The Core Problem: Chrome Ignores Proxy Credentials
When you pass a proxy string like --proxy-server="http://USERNAME:PASSWORD@gw.rotatingproxy.cc:7070" to Chrome, the browser strips the credentials and sends an unauthenticated request, resulting in a 407 error from our gateway. This is why you cannot directly use the gateway host in ChromeOptions. The standard solution is to run a small local proxy server on your machine (like 127.0.0.1:8888) that adds the authentication header and forwards traffic to the real gateway. The browser then connects to this local server without any credentials, and the local server handles the authentication transparently.
HTTP Setup for Chrome Using pproxy
Install pproxy via pip and run it as a local forwarder before launching your Selenium script. pproxy is a lightweight, pure-Python proxy tool that works well for this task. First, install it: pip install pproxy. Then, in a separate terminal or a subprocess, start the forwarder. The command format is:
pproxy -l http://127.0.0.1:8888 -r "http://gw.rotatingproxy.cc:7070#USERNAME:PASSWORD"
Replace USERNAME and PASSWORD with your actual proxy user credentials, including any targeting options. For example, to use a mobile proxy in Germany with a sticky session:
pproxy -l http://127.0.0.1:8888 -r "http://gw.rotatingproxy.cc:7070#zq4k8m2tva_net.mob_cc.de_sid.a7k2m9_ttl.20:PASSWORD"
Check the current pproxy docs for any updates to the command syntax. With the forwarder running, configure your Chrome driver to use the local proxy:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--proxy-server=http://127.0.0.1:8888')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://example.com")
All traffic from this Chrome instance will now flow through the local pproxy, which authenticates with RotatingProxy and rotates IPs according to your username options. This pattern is essential for tasks like web scraping or price monitoring where you need browser automation with clean IPs.
SOCKS5 Setup for Chrome (When Supported)
Selenium with Chrome does not natively support configuring a SOCKS5 proxy with authentication via command-line flags in a way that solves the credential problem. While you can set --proxy-server="socks5://127.0.0.1:8889", you would still need a local forwarder that speaks SOCKS5 and adds authentication to the upstream SOCKS5 connection on port 7071. pproxy can also act as a SOCKS5 forwarder. The command would be:
pproxy -l socks5://127.0.0.1:8889 -r "socks5://gw.rotatingproxy.cc:7071#USERNAME:PASSWORD"
And then in ChromeOptions: chrome_options.add_argument('--proxy-server=socks5://127.0.0.1:8889'). However, SOCKS5 in browsers can sometimes have quirks with DNS resolution. For most web automation, the HTTP proxy on port 7070 is simpler and recommended. If your use case specifically requires SOCKS5, test thoroughly. For other tools that handle SOCKS5 better, see our curl proxy or Python requests proxy guides.
Firefox Alternative With a Profile
Firefox can be configured to use an authenticated proxy directly through preferences, avoiding the need for a local forwarder in many cases. You set the proxy host, port, username, and password via the webdriver.FirefoxProfile object. Here is the complete setup:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", "gw.rotatingproxy.cc")
profile.set_preference("network.proxy.http_port", 7070)
profile.set_preference("network.proxy.ssl", "gw.rotatingproxy.cc")
profile.set_preference("network.proxy.ssl_port", 7070)
# Set credentials
profile.set_preference("network.proxy.username", "USERNAME_net.res_cc.us")
profile.set_preference("network.proxy.password", "PASSWORD")
# Important: This preference enables authentication
profile.set_preference("network.proxy.no_proxies_on", "")
driver = webdriver.Firefox(firefox_profile=profile)
driver.get("https://example.com")
This method is often more straightforward for Firefox-based automation, useful for social media management or ad verification. Note that the username string includes all targeting and session options, just as it would in the pproxy command.
Passing Targeting and Sessions Through the Username
All geo-targeting and session control is done via the proxy username, not through Selenium or browser settings. Build your username string using the underscore syntax. For example, to target Chicago, Illinois with a residential proxy and maintain a 30-minute sticky session:
username = "zq4k8m2tva_net.res_cc.us_st.il_city.chicago_sid.mysession123_ttl.30"
Use this full string as the USERNAME part in your pproxy command or Firefox profile. For rotation on every connection, omit the sid and ttl options entirely. This granular control is key for localization testing and checking geo-restricted content. Remember, city targeting is available for residential and mobile networks; see the city targeting guide for the list of available cities.
Verifying the Exit IP and Location
After launching the browser through the proxy, verify that your traffic is exiting from the expected IP and location. You can do this by having Selenium execute JavaScript to fetch your IP from a simple API. A reliable method is:
driver.get("https://api.ipify.org?format=json")
ip_json = driver.find_element("tag name", "pre").text
print("Exit IP:", ip_json)
# For more details like location
driver.get("https://ipinfo.io/json")
details = driver.find_element("tag name", "pre").text
print("Location details:", details)
This confirms your proxy is working and that your targeting options (like cc.us) are effective. It's a good first step in any automation script to catch configuration errors early.
Common Errors and Fixes
Error 407 (Proxy Authentication Required): This means your credentials are not reaching our gateway. In Chrome, you are likely trying to use the gateway directly in --proxy-server. Switch to the pproxy forwarder method. In Firefox, double-check that the network.proxy.username and .password preferences are set correctly and that the username string is exact. Also, ensure the proxy user is active and has not been paused in the dashboard. More details at proxy error 407.
Timeouts or Slow Responses: This can be due to the target site being slow, network congestion, or using a high-latency proxy network (like residential from a distant country). Try a datacenter proxy (net.dc) for lower latency. Increase the default timeouts in Selenium: driver.set_page_load_timeout(30) and driver.implicitly_wait(10). Also, ensure your local forwarder (pproxy) is not a bottleneck; it's lightweight but monitor its CPU usage. See general proxy timeouts advice.
TLS/SSL Errors: If you see "SSL handshake failed" or "ERR_TUNNEL_CONNECTION_FAILED," it's often because the browser's TLS fingerprint is being blocked by the target site, not a proxy issue. This often occurs when scraping Google or similar sophisticated sites. Try switching proxy networks (e.g., from datacenter to residential), or adjust the browser's user agent and other fingerprinting aspects. Using a real Firefox profile often has a better TLS fingerprint than headless Chrome.
Tips for Running Selenium at Scale
When automating multiple browser instances concurrently, manage your proxy sessions and concurrency limits carefully. First, understand our concurrency limit of 1,000 connections per account. Each browser driver instance typically holds one persistent connection to the proxy. For rotating sessions, use a unique sid value for each browser instance or workflow you want to isolate. For example, if you have 50 parallel browsers for market research, you could generate 50 unique session IDs.
To avoid port conflicts, run a separate pproxy forwarder on a different local port for each browser instance, or use a tool that can manage multiple upstream credentials on a single port (more advanced). Monitor your balance and traffic usage closely; browser automation consumes more bandwidth than simple HTTP requests due to images, CSS, and JavaScript. Consider disabling images and unnecessary resources in ChromeOptions to reduce proxy traffic costs.
Finally, always implement polite crawling practices: respect robots.txt (how to read robots.txt), add delays between requests, and use headless mode cautiously as some sites detect it. For large-scale, resilient scraping, consider pairing Selenium with a simpler HTTP library like requests or httpx (via httpx proxy) for non-JavaScript pages, reserving the browser for JavaScript-rendered content only.
Questions
Can I use the same proxy authentication for multiple Chrome tabs/windows?
Yes, if you launch multiple Chrome drivers or tabs that all use the same local forwarder (e.g., pproxy on 127.0.0.1:8888), they will share the upstream proxy connection and its session rules. If you want different IPs per tab, you need separate forwarders with different usernames (or no session ID for rotation) on different local ports.
Why is my Firefox proxy not working even with the profile settings?
Double-check the preference names; they are case-sensitive. Ensure you have set both the HTTP and SSL proxy hosts/ports. Also, Firefox may prompt for credentials in a dialog in some versions; the profile method should avoid this, but if you see a prompt, the password preference may not be applying. Try the latest Selenium and Firefox versions.
How do I target a specific US state like California?
Append <code>_st.ca</code> to your username, and also include <code>_cc.us</code>. For example: <code>USERNAME_net.res_cc.us_st.ca</code>. This only works with residential and mobile proxy networks. See the US state targeting guide for the list of state codes.
My session IP changed before the TTL minutes were up. Why?
The sticky session system keeps the same IP as long as the device (exit node) remains online and your connections use the same session ID. If the device goes offline, the system will move you to a new IP in the same targeted location. This ensures continuity. For more, read why my session IP changed early.
Can I use this setup with headless Chrome?
Absolutely. The proxy configuration is the same whether Chrome is headless or not. Just add <code>chrome_options.add_argument('--headless=new')</code> (for newer versions) to your ChromeOptions. Remember that headless browsers have different fingerprints and may be more easily detected by some sites.
What's the difference between using port 7070 and 7071 in this context?
Port 7070 is for HTTP/HTTPS (CONNECT) proxy traffic, which is what browsers primarily use. Port 7071 is for SOCKS5. For Selenium browser automation, port 7070 via HTTP proxy is the standard and most reliable method. The SOCKS5 port might be used for other types of traffic or tools. See which port to use.
Start with one top-up, use any network
Create an account in a minute, add $40 or more, and the same balance pays for residential, mobile, datacenter and ISP traffic.