如何在需要使用python进行身份验证的代理服务器后运行selenium Web驱动程序 [英] How to run selenium web driver behind a proxy server which needs authentication in python
问题描述
目前这是我的代码,但是webDriver显示了一个弹出窗口来输入代理凭据,我不希望出现这种烦人的情况,这不是第一次在stackoverflow中出现相同的问题,但是没有人回答正确的答案.
At present this is my code, but webDriver is showing a pop-up to enter proxy credentials and I don't want this annoying situation, This is not the first time the same question appeared in stackoverflow, but no one replied with a proper answer.
我尝试了google来找到此问题的解决方案.我开始了解Java中的解决方案,但是我不知道我们如何在python中实现它.
I tried google to find a solution for this problem. I came to know the solution in java, but i dont know how we do it in python.
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/")
以下这些陈述是我猜得出来的,我不确定它们的语法是否正确,即使我试图在硒文档中找到答案,但也没有帮助.你们能对此有所启发吗?
These below statements are guessed by me, and I am not sure whether their syntax is correct or not, even i tried to find out in selenium documentation, but no help. Would you guys throw some light on this.
#fp.set_preference("network.proxy.user_name", 'someusername')
#fp.set_preference("network.proxy.password", 'somepassword')
p.s.在此处在使用Python的硒中输入相同的问题:输入/提供http代理密码Firefox
推荐答案
Selenium无法使用基本身份验证来处理,也不能很好地与弹出窗口配合使用.但是这个问题是可以解决的.作为对我有用的解决方案(我在此处)找到了添加一个浏览器扩展,该扩展对Selenium进行身份验证.这很简单.这是适用于Chrome和Python的方式:
Selenium can't handle with basic authentication neither it works well with popups. But this problem is resolvable. As a solution that worked to me (I found it here) is to add a browser extension that does the authentication for Selenium. It's quite simple. Here is how it works for Chrome and 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: ["<all_urls>"]},
['blocking']
);
不要忘记将 YOUR_PROXY _ * 替换为您的设置.
Don't forget to replace YOUR_PROXY_* to your settings.
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"
}
- 添加创建的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示例,请转到原始文章
That's it. For me that worked like a charm. If you need to create proxy.zip dynamically or need PHP example then go to the original post
这篇关于如何在需要使用python进行身份验证的代理服务器后运行selenium Web驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!