One way to connect to a proxy server with a user name 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