Implementation of https://stackoverflow.com/a/35293284 for https://stackoverflow.com/questions/12848327/ (Run Selenium on a proxy server that requires authentication.) Solution involves creating & adding a Chromium extension at runtime. CHROMIUM-ONLY! *** Only Chrome and Edge browser
(
proxy_string,
proxy_user,
proxy_pass,
proxy_scheme="http",
bypass_list=None,
zip_it=True,
)
| 17 | |
| 18 | |
| 19 | def create_proxy_ext( |
| 20 | proxy_string, |
| 21 | proxy_user, |
| 22 | proxy_pass, |
| 23 | proxy_scheme="http", |
| 24 | bypass_list=None, |
| 25 | zip_it=True, |
| 26 | ): |
| 27 | """Implementation of https://stackoverflow.com/a/35293284 for |
| 28 | https://stackoverflow.com/questions/12848327/ |
| 29 | (Run Selenium on a proxy server that requires authentication.) |
| 30 | Solution involves creating & adding a Chromium extension at runtime. |
| 31 | CHROMIUM-ONLY! *** Only Chrome and Edge browsers are supported. *** |
| 32 | """ |
| 33 | background_js = None |
| 34 | if not bypass_list: |
| 35 | bypass_list = "" |
| 36 | if proxy_string: |
| 37 | proxy_protocol = "" |
| 38 | if proxy_string.count("://") == 1: |
| 39 | proxy_protocol = proxy_string.split("://")[0] + "://" |
| 40 | proxy_string = proxy_string.split("://")[1] |
| 41 | proxy_host = proxy_protocol + proxy_string.split(":")[0] |
| 42 | proxy_port = proxy_string.split(":")[1] |
| 43 | background_js = ( |
| 44 | """var config = {\n""" |
| 45 | """ mode: "fixed_servers",\n""" |
| 46 | """ rules: {\n""" |
| 47 | """ singleProxy: {\n""" |
| 48 | """ scheme: "%s",\n""" |
| 49 | """ host: "%s",\n""" |
| 50 | """ port: parseInt("%s")\n""" |
| 51 | """ },\n""" |
| 52 | """ bypassList: ["%s"]\n""" |
| 53 | """ }\n""" |
| 54 | """ };\n""" |
| 55 | """chrome.proxy.settings.set(""" |
| 56 | """{value: config, scope: "regular"}, function() {""" |
| 57 | """});\n""" |
| 58 | """function callbackFn(details) {\n""" |
| 59 | """ return {\n""" |
| 60 | """ authCredentials: {\n""" |
| 61 | """ username: "%s",\n""" |
| 62 | """ password: "%s"\n""" |
| 63 | """ }\n""" |
| 64 | """ };\n""" |
| 65 | """}\n""" |
| 66 | """chrome.webRequest.onAuthRequired.addListener(\n""" |
| 67 | """ callbackFn,\n""" |
| 68 | """ {urls: ["<all_urls>"]},\n""" |
| 69 | """ ['blocking']\n""" |
| 70 | """);""" % ( |
| 71 | proxy_scheme, |
| 72 | proxy_host, |
| 73 | proxy_port, |
| 74 | bypass_list, |
| 75 | proxy_user, |
| 76 | proxy_pass, |