0
我試圖使用代理服務器來啓動Selenium Chrome驅動程序。到目前爲止,我發現的唯一解決方案是使用Chrome的一種插件進行身份驗證,但它不是很可靠,所以我想知道是否還有其他選擇。Selenium和Python的代理服務器
以下是我現在用
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: "",
port: parseInt(6060)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "",
password: ""
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
"""
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.add_argument("--start-maximized")
co.add_extension(pluginfile)
driver = webdriver.Chrome(executable_path='/usr/local/bin/chromedriver')
謝謝。哪一個是Python包裝? – Christian
對不起,我錯過了鏈接。現在修復了答案。 https://github.com/AutomatedTester/browsermob-proxy-py –
thx再次。因此,這是Chrome的代碼,例如從'browsermobproxy import Server server = Server(「path/to/browsermob-proxy」) server.start() proxy = server.create_proxy()chrome_options = webdriver.ChromeOptions() chrome_options.add_argument(「 - proxy-server = {0}」.format(proxy.proxy)) browser = webdriver.Chrome(chrome_options = chrome_options)' – Christian