Created
October 24, 2016 12:10
-
-
Save equinoxel/151655b26f2ae90655e494eb5873a025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.proxy import Proxy | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
import zipfile | |
PROXY_HOST="192.168.0.10" | |
PROXY_PORT=10000 | |
PROXY_USER="user" | |
PROXY_PASS="pass" | |
def main_chrome(): | |
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: "%(host)s", | |
port: parseInt(%(port)d) | |
}, | |
bypassList: ["foobar.com"] | |
} | |
}; | |
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); | |
function callbackFn(details) { | |
return { | |
authCredentials: { | |
username: "%(user)s", | |
password: "%(pass)s" | |
} | |
}; | |
} | |
chrome.webRequest.onAuthRequired.addListener( | |
callbackFn, | |
{urls: ["<all_urls>"]}, | |
['blocking'] | |
); | |
""" % { | |
"host": PROXY_HOST, | |
"port": PROXY_PORT, | |
"user": PROXY_USER, | |
"pass": 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) | |
co = Options() | |
co.binary_location = "C:\\PGM\\bin\\chrome-win32\\chrome.exe" | |
co.add_extension(pluginfile) | |
driver = webdriver.Chrome(chrome_options=co) | |
driver.get("http://www.google.ie") | |
if __name__ == '__main__': | |
main_chrome() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Having a specific chrome binary location reduces the portability between OS. Is there a way to use ChromeDriver to install the extension instead of directly targeting the Chrome binary? http://chromedriver.chromium.org/extensions