问题 如何在需要在python中进行身份验证的代理服务器后面运行selenium web驱动程序
目前这是我的代码,但是webDriver正在显示一个输入代理凭据的弹出窗口,我不希望这种烦人的情况,这不是第一次在stackoverflow中出现相同的问题,但没有人回复正确的答案。
我试过谷歌找到解决这个问题的方法。我开始了解java中的解决方案,但我不知道我们是如何在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/")
以下这些陈述是我猜到的,我不确定他们的语法是否正确,即使我试图在selenium文档中找到,但没有帮助。你们会对此有所了解吗?
#fp.set_preference("network.proxy.user_name", 'someusername')
#fp.set_preference("network.proxy.password", 'somepassword')
附:这里问同样的问题 使用Python的Selenium:为firefox输入/提供http代理密码
1982
2017-10-11 20:51
起源
答案:
Selenium无法处理基本身份验证,也不适用于弹出窗口。但这个问题是可以解决的。作为一个对我有用的解决方案(我找到了它 这里)是添加一个浏览器扩展,为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: ["<all_urls>"]},
['blocking']
);
别忘了更换 YOUR_PROXY_ * 到你的设置。
的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示例,请转到 原帖
8
2018-02-09 13:25
答案:
Selenium无法处理基本身份验证,也不适用于弹出窗口。但这个问题是可以解决的。作为一个对我有用的解决方案(我找到了它 这里)是添加一个浏览器扩展,为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: ["<all_urls>"]},
['blocking']
);
别忘了更换 YOUR_PROXY_ * 到你的设置。
的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示例,请转到 原帖
8
2018-02-09 13:25
我知道它很晚才回复你的问题,但最近我开始使用Python,并试图做同样的事情并做了类似的事情来处理这种情况。
在代理服务器后面运行selenium web驱动程序
- 需要创建一个firefox配置文件,其中应安装“autoauth”附加组件。
- 尝试通过手动点击URL来保存代理服务器用户名和密码。
- Firefox配置文件将在autoauth的帮助下保存代理服务器的凭据
- 在脚本中调用该特定的Firefox配置文件。
- 设置所有首选项以定义代理服务器详细信息。
- 将Firefox配置文件分配到浏览器的实例中
- 点击任何URL,下面是正在运行的示例
附: : 从Internet选项中删除所有代理设置,脚本将自动使用它
所以在技术上你不会发送代理用户名和密码,你将保存这些凭证在firefox中并调用该特定的firefox配置文件。
希望你早已解决了你的问题,但如果它仍然存在,这可能会对你有所帮助。 :)
3
2018-05-08 09:26
有关如何访问firefox配置文件,请参阅此路径:
http://software-testing-tutorials-automation.blogspot.jp/2014/05/how-to-create-and-use-custom-firefox.html
基本上你需要创建一个配置文件并在firefox中使用该配置文件。
现在,当您通过selenium运行时,您的个人资料的行为也将是相同的。因此,如果您的配置文件将提示代理身份验证,那么当您通过selenium打开它时它也会显示。
因此,您必须通过使用加载项来使用该配置文件隐藏身份验证。成功完成后,Selenium现在也是如此。
0
2017-09-11 03:28