You can create a Chrome extension that can handle the proxy on the fly.
ChromeDriver does not provide any capability to handle HTTP proxy that needs credentials.
Create a zip file proxyExtension.zip
that contains the following 2 files;
background.js
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOU_PROXY_ADDRESS",
port: parseInt(YOUR_PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_PROXY_USERNAME",
password: "YOUR_PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
YOU_PROXY_ADDRESS
, YOUR_PROXY_PORT
, YOUR_PROXY_USERNAME
, YOUR_PROXY_PASSWORD
fields will be replaced with your informations.
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"]
},
"minimum_chrome_version":"22.0.0"
}
Then, initialize the webdriver with the following code;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addExtensions(new File("path_to_extension_file/proxyExtension.zip"));
WebDriver driver = new ChromeDriver(chromeOptions);
Please change the path_to_extension_file
to your directory that has the proxyExtension.zip
file.
You can also find more information on the link.