Created
October 19, 2022 13:20
-
-
Save danbailo/37cfdc0a5a86396be8b187613fec9fc7 to your computer and use it in GitHub Desktop.
Setting a proxy selenium(docker) with auth
This file contains hidden or 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.webdriver import Remote, ChromeOptions | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
import zipfile | |
PROXY_HOST = '192.168.3.2' # rotating proxy or host | |
PROXY_PORT = 8080 # port | |
PROXY_USER = 'proxy-user' # username | |
PROXY_PASS = 'proxy-password' # password | |
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) | |
chrome_options = ChromeOptions() | |
pluginfile = 'proxy_auth_plugin.zip' | |
with zipfile.ZipFile(pluginfile, 'w') as zp: | |
zp.writestr("manifest.json", manifest_json) | |
zp.writestr("background.js", background_js) | |
chrome_options.add_argument('--start-maximized') | |
chrome_options.add_argument('--ignore-certificate-errors') | |
chrome_options.add_extension(pluginfile) | |
driver = Remote( | |
command_executor='http://127.0.0.1:4444', | |
desired_capabilities=DesiredCapabilities.CHROME, | |
options=chrome_options | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment