def getPlugin(proxy_host, proxy_port, proxy_user, proxy_pass):
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"
}
"""
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_host, proxy_port, proxy_user, proxy_pass)
pluginfile = 'proxy_auth_plugin.zip'
with zipfile.ZipFile(pluginfile, 'w') as zp:
zp.writestr("manifest.json", manifest_json)
zp.writestr("background.js", background_js)
return pluginfile
proxyArgsList = [
{
'proxy_host': '192.168.1.1',
'proxy_port': '80',
'proxy_user': 'abc',
'proxy_pass': 'abcpassword',
},
{
'proxy_host': '192.168.1.2',
'proxy_port': '80',
'proxy_user': 'abc',
'proxy_pass': 'abcpassword',
},
'proxy_host': '192.168.1.3',
'proxy_port': '80',
'proxy_user': 'abc',
'proxy_pass': 'abcpassword',
]
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_extension(getPlugin(**random.choice(proxyArgsList)))
driver = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=chromeOptions)