How to Connect to a Proxy Server with Username and Password using Chromedriver

Daniel Delimata
2 min readFeb 20, 2023

Sometimes there is a need to connect to a proxy server with a username and password (i.e. USERNAME:PASSWD@IP:PORT) with chromedriver WebDriver in selenium in java. It is simple without username and password protection, but may be more difficult with it.

One way to connect to a proxy server with a username and password is by using the tool sshuttle running on your machine. This tool allows you to create a connection to your proxy server using SSH, and then route all of your traffic.

sshuttle -r username@remotehost 0.0.0.0/0

See https://sshuttle.readthedocs.io/en/stable/usage.html for further information.

Another approach is to add a Chrome extension. This approach is a bit more complex, but it allows you to use the Chrome WebDriver without any tools running in the background. You would need to create a Chrome extension by including two files in an archive named proxy.zip: Background.js and manifest.js.

Background.js:

var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOUR_PROXY_ADDRESS",
port: parseInt(PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({ value: config, scope: "regular" }, function () { });
function callbackFn(details) {
return {
authCredentials: {
username: "PROXY_USERNAME",
password: "PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{ urls: ["<all_urls>"] },
['blocking']
);

manifest.js:

var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOUR_PROXY_ADDRESS",
port: parseInt(PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({ value: config, scope: "regular" }, function () { });
function callbackFn(details) {
return {
authCredentials: {
username: "PROXY_USERNAME",
password: "PROXY_PASSWORD"
}{
"version": "1.0.0",
"manifest_version": 3,
"name": "Chrome Proxy",
"permissions": [
"Proxy",
"Tabs",
"unlimitedStorage",
"Storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"Minimum_chrome_version":"76.0.0"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{ urls: ["<all_urls>"] },
['blocking']
);

Then you would need to add the Chrome extension to the ChromeOptions using the addExtension method:

ChromeOptions options = new ChromeOptions();
options.addExtensions("proxy.zip");

The details are described (for Python) on the page: https://www.browserstack.com/guide/set-proxy-in-selenium

The story was originally created by me, but it may contain parts that were created with AI assistance. My original text has been corrected and partially rephrased by Chat Generative Pre-trained Transformer to improve the language.

--

--

Daniel Delimata

Test Automation Engineer | Typesetter | Testing Teacher