How can I setup Selenium proxy?
Hello Alex,
You can refer to the sample code below to set up Selenium proxy:
/// <summary>
/// Performs the initialize of the browser.
/// </summary>
/// <param name="driverFolder">The driver folder.</param>
/// <param name="proxy">The proxy.</param>
/// <returns>
/// The web driver.
/// </returns>
protected override IWebDriver PerformInitialize(string driverFolder, Proxy proxy)
{
var options = new ChromeOptions();
if (!string.IsNullOrWhiteSpace(proxy.HttpProxy))
{
options.AddAdditionalCapability(CapabilityType.Proxy, proxy);
}
return new ChromeDriver(driverFolder, options);
}
Setting up a proxy in Selenium involves telling the browser, not just the driver, to route its traffic through an intermediary server. The method varies slightly by programming language and browser.
- Python with Chrome (Using selenium-wire for Full Control) The standard selenium package can handle basic proxies, but selenium-wire extends it to easily capture requests, handle authentication, and more.
First, install it:
bash pip install selenium-wire Code Example:
python from seleniumwire import webdriver
Define proxy options
proxy_options = { ‘proxy’: { ‘http’: ‘http://user:pass@proxy.momoproxy.com:8100’, ‘https’: ‘https://user:pass@proxy.momoproxy.com:8100’, ‘no_proxy’: ‘localhost,127.0.0.1’ # Exclusions } }
Create the driver with proxy options
driver = webdriver.Chrome(seleniumwire_options=proxy_options)
Now all traffic goes through the proxy
driver.get(“https://httpbin.org/ip”) print(driver.page_source) # Will show the proxy’s IP, not yours
driver.quit() Why this is different: It effortlessly handles proxy authentication (user:pass) and gives you the power to intercept and modify network requests later.
- JavaScript (WebDriverIO) with Chrome WebDriverIO has a clean, declarative syntax for capabilities.
Code Example:
javascript const { remote } = require(‘webdriverio’);
(async () => { const driver = await remote({ capabilities: { browserName: ‘chrome’, proxy: { proxyType: ‘manual’, httpProxy: ‘proxy.momoproxy.com:8100’, sslProxy: ‘proxy.momoproxy.com:8100’, } } });
await driver.url('https://httpbin.org/ip');
const body = await driver.$('body');
console.log(await body.getText());
await driver.deleteSession();
})(); Why this is different: It shows the W3C WebStandard-compliant way of setting proxies, which is the future of Selenium configuration.
- Java with Firefox - A Different Browser & Approach The original example was for Chrome in C#. Here’s how to do it for Firefox in Java, which uses a different settings object.
Code Example:
java import org.openqa.selenium.Proxy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions;
public class FirefoxProxyExample { public static void main(String args) { // Define the Proxy Proxy proxy = new Proxy(); proxy.setHttpProxy(“proxy.momoproxy.com:8100”); proxy.setSslProxy(“proxy.momoproxy.com:8100”);
// Configure Firefox Options
FirefoxOptions options = new FirefoxOptions();
options.setCapability("proxy", proxy);
// Launch Firefox with the proxy
WebDriver driver = new FirefoxDriver(options);
driver.get("https://httpbin.org/ip");
driver.quit();
}
} Why this is different: It demonstrates proxy setup for Firefox in Java, highlighting that the core concept (injecting a proxy capability) is consistent, but the implementation classes differ.
- Handling Proxy Authentication (A Common Challenge) The standard Proxy class in Selenium cannot handle authentication pop-ups. You must use a browser extension or a separate automation tool. Here’s a Python example using an extension for Chrome:
python from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType
Define proxy with auth (using a plugin)
proxy = “proxy.momoproxy.com:8100” username = “your_username” password = “your_password”
manifest_json = “”" { “version”: “1.0.0”, “manifest_version”: 2, “name”: “Chrome Proxy”, “permissions”: [ “proxy”, “tabs”, “unlimitedStorage”, “storage”, “<all_urls>”, “webRequest”, “webRequestBlocking” ], “background”: { “scripts”: [“background.js”] } } “”"
background_js = “”" var config = { mode: “fixed_servers”, rules: { singleProxy: { scheme: “http”, host: “%s”, port: parseInt(%s) }, bypassList: [“localhost”] } };
chrome.proxy.settings.set({value: config, scope: “regular”}, function() {});
function callbackFn(details) { return { authCredentials: { username: “%s”, password: “%s” } }; }
chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: [“<all_urls>”]}, [‘blocking’] ); “”" % (proxy, port, username, password)
chrome_options = webdriver.ChromeOptions() chrome_options.add_extension(BasePackedExtension(manifest_json, background_js))
driver = webdriver.Chrome(options=chrome_options) driver.get(“https://momoproxy/ip”) Why this is different: This directly addresses the biggest pain point in Selenium proxy setup—authentication—which the original code completely ignored.
In summary, while the core principle is to pass proxy settings as a capability, the “how” depends greatly on your specific needs: the language, the browser, and whether you need authentication or advanced traffic control.