How to Connect to a Proxy Server with Username and Password using Chromedriver
--
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>"…