![](/img/trans.png)
[英]How to login in a proxy server with authentication with selenium chrome driver Python 3?
[英]Proxy server for Selenium and Python
我正在尝试使用代理服务器来启动Selenium Chrome驱动程序。 到目前为止,我发现的唯一解决方案是使用一种用于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: "",
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')
我认为插件不是一个很好的解决方案,当纯HTTP解决方案在所有浏览器,版本和操作系统上均能正常工作时,当然不是。
您可以将此Python包装器用于Browsermob , Browsermob是用Java编写的非常著名的独立或嵌入式代理服务器。
该代码显示了headers()
Python方法如何用于添加标头,而无需对REST API进行POST。 测试中有一些使用示例 ,例如:
self.client = Client("localhost:9090")
self.client.headers({'User-Agent': 'rubber ducks floating in a row'})
self.client.headers({'Authorization': 'Basic dXNlcjpwYXNz'}) # User 'user', password 'pass'
更新:
只是为了阐明WebDriver和代理如何配合使用:
proxy
对象。 Proxy
对象,然后将其添加到DesiredCapabilities
对象,然后按照此示例那样创建驱动程序。 从那时起,代理侦听是自动的,您可以开始创建HAR文件。
另外,对于使用Twisted的自定义Python代理服务器,您可能会看到此答案 。
我在这里对Python中的Selenium代理有更长的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.