如何在需要python身份验证的代理服务器后面运行selenium web驱动程序
目前这是我的代码,但是webDriver显示一个弹出窗口来输入代理凭据,我不希望出现这种恼人的情况,这不是stackoverflow中第一次出现相同的问题,但是没有人给出正确的答案 我试着用谷歌来解决这个问题。我开始了解java中的解决方案,但我不知道如何使用python如何在需要python身份验证的代理服务器后面运行selenium web驱动程序,python,authentication,selenium,proxy,Python,Authentication,Selenium,Proxy,目前这是我的代码,但是webDriver显示一个弹出窗口来输入代理凭据,我不希望出现这种恼人的情况,这不是stackoverflow中第一次出现相同的问题,但是没有人给出正确的答案 我试着用谷歌来解决这个问题。我开始了解java中的解决方案,但我不知道如何使用python PROXY_HOST = "65.49.1.59" PROXY_PORT = 60099 fp = webdriver.FirefoxProfile() # Direct = 0, Manual
PROXY_HOST = "65.49.1.59"
PROXY_PORT = 60099
fp = webdriver.FirefoxProfile()
# Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5
print " im in parse_details"
fp.set_preference("network.proxy.type", 1)
fp.set_preference('network.http.phishy-userpass-length', 255)
fp.set_preference("network.proxy.http", PROXY_HOST)
fp.set_preference("network.proxy.http_port", PROXY_PORT)
fp.set_preference("network.proxy.ftp", PROXY_HOST)
fp.set_preference("network.proxy.ftp_port", PROXY_PORT)
fp.set_preference("network.proxy.ssl", PROXY_HOST)
fp.set_preference("network.proxy.ssl_port", PROXY_PORT)
#fp.set_preference("network.proxy.user_name", 'someusername')
#fp.set_preference("network.proxy.password", 'somepassword')
fp.set_preference("network.proxy.no_proxies_on", "") # set this value as desired
self.driver = webdriver.Firefox(firefox_profile=fp)
self.driver.get("http://www.whatismyip.com/")
下面这些语句是我猜出来的,我不确定它们的语法是否正确,即使我试图在selenium文档中找到它们,但没有任何帮助。你们能解释一下吗
#fp.set_preference("network.proxy.user_name", 'someusername')
#fp.set_preference("network.proxy.password", 'somepassword')
p、 我知道现在回答你的问题已经很晚了,但是最近我开始使用Python,并且尝试着做同样的事情,并且做了类似的事情来处理这种情况 在代理服务器后面运行selenium web驱动程序
希望您很久以前就已经解决了问题,但如果问题仍然存在,这可能会对您有所帮助。:) 有关如何访问firefox配置文件,请参阅以下路径: 基本上,您需要创建一个配置文件并在firefox中使用该配置文件。 现在,当您通过selenium运行时,配置文件的行为也将相同。所以,如果您的配置文件将提示proxy auth,那么当您通过selenium打开它时,它也会显示出来。
因此,您必须找到一种方法,使用附加组件隐藏使用该配置文件的身份验证。成功完成此操作后,Selenium现在也将如此。Selenium无法处理基本身份验证,也无法处理弹出窗口。但这个问题是可以解决的。作为一个对我有效的解决方案(我发现了它),我添加了一个浏览器扩展,该扩展对Selenium进行身份验证。这很简单。以下是Chrome和Python的工作原理:
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: ["<all_urls>"]},
['blocking']
);
就这样。对我来说,这很有魅力。如果您需要动态创建proxy.zip或需要PHP示例,那么请转到警告,提醒任何想为Firefox这样做的人:Firefox不支持onAuthRequired。@XaviLópez,您在说什么?这是到目前为止我找到的唯一解决方案。@XaviLópez好的,很酷,我也会清理评论。但这太疯狂了,我找不到任何能与Chrome代理身份验证一起工作的东西……不幸的是,这在无头模式下不起作用:什么去了?应该更换吗?
{
"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"
}
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()