硒不能处理基本认证,它也不适用于弹出窗口.但这个问题是可以解决的.作为一个对我有用的解决方案(我发现它为
here)是添加一个浏览器扩展,为Selenium进行身份验证.这很简单以下是Chrome和Python的工作原理:
>创建包含两个文件的zip文件proxy.zip:
background.js
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "YOU_PROXY_ADDRESS",
port: parseInt(YOUR_PROXY_PORT)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_PROXY_USERNAME",
password: "YOUR_PROXY_PASSWORD"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: [""]},
['blocking']
);
不要忘记将YOUR_PROXY_ *替换为您的设置.
的manifest.json
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Proxy",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
>添加创建的proxy.zip作为扩展名
Python代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_extension("proxy.zip")
driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
driver.get("http://google.com")
driver.close()
而已.对我来说,像一个魅力一样.如果您需要动态创建proxy.zip或需要PHP示例,请转到original post