Selenium proxy setup involves configuring browser options or capabilities to route web traffic through a specified proxy server, enabling tasks like geo-unblocking, anonymity, and network traffic inspection. This configuration is essential for automating tasks that require bypassing geographical restrictions, testing website behavior from different network locations, or managing network traffic during testing or data extraction.
Using a proxy with Selenium enables redirecting all browser traffic through an intermediary server. This capability is critical for:
* Bypassing geographical content restrictions.
* Maintaining anonymity during automated web interactions.
* Testing how a website performs or behaves when accessed from various network points.
* Monitoring or debugging network requests and responses.
* Accessing internal or restricted networks.
General Proxy Concepts
Before configuring Selenium, understanding fundamental proxy concepts is crucial.
Proxy Types
- HTTP/HTTPS Proxies: These proxies operate at the application layer (Layer 7). HTTP proxies handle unencrypted HTTP traffic, while HTTPS proxies handle encrypted HTTPS traffic. Many HTTPS proxies perform SSL interception (Man-in-the-Middle), decrypting traffic, inspecting it, and then re-encrypting it before forwarding. This can sometimes lead to SSL certificate errors if the proxy's certificate is not trusted by the browser.
- SOCKS Proxies (SOCKS4, SOCKS5): SOCKS (Socket Secure) proxies operate at a lower level (Layer 5). They are protocol-agnostic, meaning they can handle any type of TCP/UDP traffic, not just HTTP/HTTPS. SOCKS5 is the more common version, supporting authentication and UDP traffic, making it versatile for various applications beyond web browsing.
Proxy Authentication
Many proxy servers require authentication (username and password) to grant access. Selenium handles basic authentication by embedding credentials directly within the proxy string, typically in the format username:password@host:port.
Browser Options/Capabilities
Selenium interacts with web browsers via specific driver executables (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox). These drivers accept an Options object (e.g., ChromeOptions, FirefoxOptions) where proxy settings are defined. These settings are passed to the browser upon launch, ensuring all subsequent traffic from that browser instance is routed through the specified proxy.
Selenium Proxy Setup: Language-Specific Examples
The following sections detail how to configure proxies for Chrome and Firefox using Python, Java, and C#. The principles are transferable to other browsers supported by Selenium, such as Edge or Safari, by using their respective Options objects.
Python
Python leverages selenium.webdriver.Proxy and ProxyType to define proxy configurations within browser options.
Basic HTTP/HTTPS Proxy
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
# Proxy settings
PROXY_HOST = "your_proxy_host"
PROXY_PORT = 8080 # Or 443 for HTTPS
# --- Chrome Example ---
chrome_options = ChromeOptions()
chrome_options.add_argument(f'--proxy-server={PROXY_HOST}:{PROXY_PORT}')
# Optional: Disable SSL certificate checks if proxy causes issues (use with caution)
# chrome_options.add_argument('--ignore-certificate-errors')
driver_chrome = webdriver.Chrome(options=chrome_options)
driver_chrome.get("http://www.example.com")
# ... perform actions ...
driver_chrome.quit()
# --- Firefox Example ---
firefox_options = FirefoxOptions()
firefox_options.set_preference("network.proxy.type", 1) # Manual proxy configuration
firefox_options.set_preference("network.proxy.http", PROXY_HOST)
firefox_options.set_preference("network.proxy.http_port", PROXY_PORT)
firefox_options.set_preference("network.proxy.ssl", PROXY_HOST)
firefox_options.set_preference("network.proxy.ssl_port", PROXY_PORT)
firefox_options.set_preference("network.proxy.no_proxies_on", "") # Ensure proxy is used for all hosts
driver_firefox = webdriver.Firefox(options=firefox_options)
driver_firefox.get("http://www.example.com")
# ... perform actions ...
driver_firefox.quit()
Authenticated HTTP/HTTPS Proxy
For authenticated HTTP/HTTPS proxies, embed the credentials directly into the proxy string.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
PROXY_HOST = "your_proxy_host"
PROXY_PORT = 8080
PROXY_USER = "your_username"
PROXY_PASS = "your_password"
# --- Chrome Example ---
# Format: user:pass@host:port
authenticated_proxy = f"{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}"
chrome_options = ChromeOptions()
chrome_options.add_argument(f'--proxy-server={authenticated_proxy}')
driver_chrome = webdriver.Chrome(options=chrome_options)
driver_chrome.get("http://www.example.com")
driver_chrome.quit()
# --- Firefox Example ---
# Firefox's native preferences do not directly support username/password in the proxy string
# for HTTP/HTTPS. A common workaround is to use a browser extension or a proxy manager.
# However, if using the "direct" proxy option through the capabilities (which is less common for Firefox
# compared to Chrome's --proxy-server argument), it might work.
# For robust Firefox authenticated proxy, consider using a proxy extension or a SOCKS5 proxy.
# The following is a general approach that might not work for all HTTP/HTTPS authenticated proxies in Firefox directly.
# For simplicity and cross-browser consistency, consider using SOCKS5 if authentication is a must.
# The standard Firefox preferences do not directly support embedding user:pass for HTTP/HTTPS.
# For authenticated HTTP/HTTPS proxies in Firefox, the browser typically prompts for credentials.
# WebDriver cannot interact with these native browser authentication prompts directly.
# A common solution is to use a proxy that supports authentication via the URL (like some SOCKS5 proxies)
# or to use a browser extension that handles proxy authentication.
# For this example, we'll omit a direct Firefox authenticated HTTP/HTTPS proxy setup due to complexity
# and lack of direct WebDriver support for native browser authentication prompts.
SOCKS Proxy
SOCKS proxies require specifying the SOCKS version. SOCKS5 is generally preferred due to its features.
from selenium import webdriver
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
PROXY_HOST = "your_socks_proxy_host"
PROXY_PORT = 1080 # Common SOCKS port
PROXY_USER = "your_username" # Optional for SOCKS5
PROXY_PASS = "your_password" # Optional for SOCKS5
# --- Chrome Example ---
chrome_options = ChromeOptions()
# For SOCKS5 with authentication, format is socks5://user:pass@host:port
# For SOCKS5 without authentication, format is socks5://host:port
# For SOCKS4, use socks4://host:port
socks_proxy_string = f"socks5://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}" if PROXY_USER else f"socks5://{PROXY_HOST}:{PROXY_PORT}"
chrome_options.add_argument(f'--proxy-server={socks_proxy_string}')
driver_chrome = webdriver.Chrome(options=chrome_options)
driver_chrome.get("http://www.example.com")
driver_chrome.quit()
# --- Firefox Example ---
firefox_options = FirefoxOptions()
firefox_options.set_preference("network.proxy.type", 1) # Manual proxy configuration
firefox_options.set_preference("network.proxy.socks", PROXY_HOST)
firefox_options.set_preference("network.proxy.socks_port", PROXY_PORT)
firefox_options.set_preference("network.proxy.socks_version", 5) # For SOCKS5
firefox_options.set_preference("network.proxy.socks_username", PROXY_USER) # For authenticated SOCKS5
firefox_options.set_preference("network.proxy.socks_password", PROXY_PASS) # For authenticated SOCKS5
firefox_options.set_preference("network.proxy.no_proxies_on", "")
driver_firefox = webdriver.Firefox(options=firefox_options)
driver_firefox.get("http://www.example.com")
driver_firefox.quit()
Java
Java uses the org.openqa.selenium.Proxy class and sets it within the capabilities of the browser driver.
Basic HTTP/HTTPS Proxy
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class SeleniumProxySetup {
public static void main(String[] args) {
String PROXY_HOST = "your_proxy_host";
int PROXY_PORT = 8080;
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxyChrome = new Proxy();
proxyChrome.setHttpProxy(PROXY_HOST + ":" + PROXY_PORT);
proxyChrome.setSslProxy(PROXY_HOST + ":" + PROXY_PORT); // Also set for HTTPS
chromeOptions.setCapability("proxy", proxyChrome);
WebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.get("http://www.example.com");
// ... perform actions ...
driverChrome.quit();
// --- Firefox Example ---
FirefoxOptions firefoxOptions = new FirefoxOptions();
Proxy proxyFirefox = new Proxy();
proxyFirefox.setHttpProxy(PROXY_HOST + ":" + PROXY_PORT);
proxyFirefox.setSslProxy(PROXY_HOST + ":" + PROXY_PORT); // Also set for HTTPS
firefoxOptions.setCapability("proxy", proxyFirefox);
WebDriver driverFirefox = new FirefoxDriver(firefoxOptions);
driverFirefox.get("http://www.example.com");
// ... perform actions ...
driverFirefox.quit();
}
}
Authenticated HTTP/HTTPS Proxy
Similar to Python, for basic HTTP/HTTPS authentication, the credentials are embedded in the proxy string.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
// Firefox example for authenticated HTTP/HTTPS is complex due to native prompts.
// Consider SOCKS5 or a browser extension for Firefox authentication.
public class SeleniumAuthenticatedProxySetup {
public static void main(String[] args) {
String PROXY_HOST = "your_proxy_host";
int PROXY_PORT = 8080;
String PROXY_USER = "your_username";
String PROXY_PASS = "your_password";
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxyChrome = new Proxy();
// Format: user:pass@host:port
String authenticatedProxyString = PROXY_USER + ":" + PROXY_PASS + "@" + PROXY_HOST + ":" + PROXY_PORT;
proxyChrome.setHttpProxy(authenticatedProxyString);
proxyChrome.setSslProxy(authenticatedProxyString); // Also set for HTTPS
chromeOptions.setCapability("proxy", proxyChrome);
WebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.get("http://www.example.com");
driverChrome.quit();
// --- Firefox Example ---
// As noted in Python, Firefox's native preferences do not directly support
// embedding username/password for HTTP/HTTPS proxies via WebDriver capabilities
// without a browser extension. The browser typically prompts for credentials.
// For robust Firefox authenticated proxy, consider using a SOCKS5 proxy or a proxy extension.
}
}
SOCKS Proxy
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class SeleniumSocksProxySetup {
public static void main(String[] args) {
String PROXY_HOST = "your_socks_proxy_host";
int PROXY_PORT = 1080; // Common SOCKS port
String PROXY_USER = "your_username"; // Optional for SOCKS5
String PROXY_PASS = "your_password"; // Optional for SOCKS5
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxyChrome = new Proxy();
// For SOCKS5 with authentication, format is socks5://user:pass@host:port
// For SOCKS5 without authentication, format is socks5://host:port
String socksProxyString = PROXY_USER != null && !PROXY_USER.isEmpty()
? "socks5://" + PROXY_USER + ":" + PROXY_PASS + "@" + PROXY_HOST + ":" + PROXY_PORT
: "socks5://" + PROXY_HOST + ":" + PROXY_PORT;
chromeOptions.addArguments("--proxy-server=" + socksProxyString); // Chrome uses --proxy-server argument for SOCKS
WebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.get("http://www.example.com");
driverChrome.quit();
// --- Firefox Example ---
FirefoxOptions firefoxOptions = new FirefoxOptions();
Proxy proxyFirefox = new Proxy();
proxyFirefox.setSocksProxy(PROXY_HOST + ":" + PROXY_PORT);
proxyFirefox.setSocksVersion(5); // For SOCKS5
// Firefox preferences for SOCKS authentication
firefoxOptions.setPreference("network.proxy.socks_username", PROXY_USER);
firefoxOptions.setPreference("network.proxy.socks_password", PROXY_PASS);
firefoxOptions.setCapability("proxy", proxyFirefox);
WebDriver driverFirefox = new FirefoxDriver(firefoxOptions);
driverFirefox.get("http://www.example.com");
driverFirefox.quit();
}
}
C
C# uses the OpenQA.Selenium.Proxy class and sets it within the browser options.
Basic HTTP/HTTPS Proxy
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
public class SeleniumProxySetup
{
public static void Main(string[] args)
{
string PROXY_HOST = "your_proxy_host";
int PROXY_PORT = 8080;
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxyChrome = new Proxy();
proxyChrome.HttpProxy = $"{PROXY_HOST}:{PROXY_PORT}";
proxyChrome.SslProxy = $"{PROXY_HOST}:{PROXY_PORT}"; // Also set for HTTPS
chromeOptions.Proxy = proxyChrome;
IWebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.Navigate().GoToUrl("http://www.example.com");
// ... perform actions ...
driverChrome.Quit();
// --- Firefox Example ---
FirefoxOptions firefoxOptions = new FirefoxOptions();
Proxy proxyFirefox = new Proxy();
proxyFirefox.HttpProxy = $"{PROXY_HOST}:{PROXY_PORT}";
proxyFirefox.SslProxy = $"{PROXY_HOST}:{PROXY_PORT}"; // Also set for HTTPS
firefoxOptions.Proxy = proxyFirefox;
IWebDriver driverFirefox = new FirefoxDriver(firefoxOptions);
driverFirefox.Navigate().GoToUrl("http://www.example.com");
// ... perform actions ...
driverFirefox.Quit();
}
}
Authenticated HTTP/HTTPS Proxy
For basic authentication, embed credentials in the proxy string.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
// Firefox example for authenticated HTTP/HTTPS is complex due to native prompts.
// Consider SOCKS5 or a browser extension for Firefox authentication.
public class SeleniumAuthenticatedProxySetup
{
public static void Main(string[] args)
{
string PROXY_HOST = "your_proxy_host";
int PROXY_PORT = 8080;
string PROXY_USER = "your_username";
string PROXY_PASS = "your_password";
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
Proxy proxyChrome = new Proxy();
// Format: user:pass@host:port
string authenticatedProxyString = $"{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}";
proxyChrome.HttpProxy = authenticatedProxyString;
proxyChrome.SslProxy = authenticatedProxyString; // Also set for HTTPS
chromeOptions.Proxy = proxyChrome;
IWebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.Navigate().GoToUrl("http://www.example.com");
driverChrome.Quit();
// --- Firefox Example ---
// As noted in Python and Java, Firefox's native preferences do not directly support
// embedding username/password for HTTP/HTTPS proxies via WebDriver capabilities
// without a browser extension. The browser typically prompts for credentials.
// For robust Firefox authenticated proxy, consider using a SOCKS5 proxy or a proxy extension.
}
}
SOCKS Proxy
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
public class SeleniumSocksProxySetup
{
public static void Main(string[] args)
{
string PROXY_HOST = "your_socks_proxy_host";
int PROXY_PORT = 1080; // Common SOCKS port
string PROXY_USER = "your_username"; // Optional for SOCKS5
string PROXY_PASS = "your_password"; // Optional for SOCKS5
// --- Chrome Example ---
ChromeOptions chromeOptions = new ChromeOptions();
// Chrome uses --proxy-server argument for SOCKS.
// For SOCKS5 with authentication, format is socks5://user:pass@host:port
// For SOCKS5 without authentication, format is socks5://host:port
string socksProxyString = string.IsNullOrEmpty(PROXY_USER)
? $"socks5://{PROXY_HOST}:{PROXY_PORT}"
: $"socks5://{PROXY_USER}:{PROXY_PASS}@{PROXY_HOST}:{PROXY_PORT}";
chromeOptions.AddArgument($"--proxy-server={socksProxyString}");
IWebDriver driverChrome = new ChromeDriver(chromeOptions);
driverChrome.Navigate().GoToUrl("http://www.example.com");
driverChrome.Quit();
// --- Firefox Example ---
FirefoxOptions firefoxOptions = new FirefoxOptions();
Proxy proxyFirefox = new Proxy();
proxyFirefox.SocksProxy = $"{PROXY_HOST}:{PROXY_PORT}";
proxyFirefox.SocksVersion = 5; // For SOCKS5
// Firefox preferences for SOCKS authentication
firefoxOptions.SetPreference("network.proxy.socks_username", PROXY_USER);
firefoxOptions.SetPreference("network.proxy.socks_password", PROXY_PASS);
firefoxOptions.Proxy = proxyFirefox;
IWebDriver driverFirefox = new FirefoxDriver(firefoxOptions);
driverFirefox.Navigate().GoToUrl("http://www.example.com");
driverFirefox.Quit();
}
}
Proxy Type Comparison
| Feature | HTTP Proxy | HTTPS Proxy | SOCKS Proxy (SOCKS5) |
|---|---|---|---|
| Protocol Layer | Application (Layer 7) | Application (Layer 7) | Session (Layer 5) |
| Traffic Handled | HTTP traffic only | Encrypted HTTP (HTTPS) traffic only | Any TCP/UDP traffic |
| Encryption | None (client-to-proxy) | Client-to-proxy (can re-encrypt) | None (client-to-proxy), application-level encryption still works |
| Anonymity | Can reveal original IP in headers | Better, but still a proxy | High, more protocol-agnostic, less header manipulation |
| Authentication | Basic, Digest (via headers) | Basic, Digest (via headers) | Username/Password, GSS-API |
| Use Case | Simple web browsing, caching, content filtering | Secure web browsing, sensitive data, SSL inspection | General purpose, P2P, gaming, VPN-like functionality |
| Performance | Generally faster for HTTP | Overhead for encryption/decryption | Can be slower due to lower-level handling |
Common Issues and Troubleshooting
- Proxy Not Working: Verify the proxy host and port. Ensure the proxy server is online and accessible from the machine running Selenium. Firewall rules might block connections to the proxy.
- Authentication Failure: Double-check the username and password. Confirm the proxy supports the authentication method used by Selenium (typically basic authentication via URL embedding).
- SSL Certificate Errors: If an HTTPS proxy performs SSL interception, the browser might report certificate warnings because it doesn't trust the proxy's dynamically generated certificate. Options include:
- Disabling certificate validation (e.g.,
--ignore-certificate-errorsfor Chrome), but this is a security risk and not recommended for production. - Installing the proxy's root certificate into the browser's trust store.
- Disabling certificate validation (e.g.,
- Timeout Errors: The proxy server might be slow, overloaded, or experiencing network issues. Increase Selenium's implicit or explicit wait times, or test with a different proxy.
- Browser-Specific Quirks: While the general approach is consistent, minor differences exist in how each browser driver handles proxy settings. Refer to browser-specific documentation if encountering unexpected behavior.
Best Practices
- Environment Variables for Credentials: Avoid hardcoding sensitive proxy usernames and passwords directly in the script. Instead, retrieve them from environment variables or a secure configuration management system.
- Dedicated Proxy Manager: For complex scenarios, such as rotating proxies, managing large proxy lists, or implementing advanced proxy logic, consider using a dedicated proxy manager library or a commercial proxy service's SDK.
- Error Handling: Implement robust
try-catchblocks to gracefully handle proxy connection failures, authentication errors, or network timeouts. - Headless Mode Compatibility: Proxy configurations work seamlessly with headless browser modes, which is beneficial for server-side automation.