How to override basic authentication in selenium2 with Java using chrome driver?(如何使用 chrome 驱动程序使用 Java 覆盖 selenium2 中的基本身份验证?)
问题描述
如何覆盖 selenium2 chrome 驱动程序中的基本身份验证?我在我的项目中遇到了一个问题,其中 chrome 需要身份验证"弹出窗口正在阻止 webdriver 继续导航.请找到所附的屏幕截图.我正在使用以下代码来实例化 chrome 驱动程序,
How to override basic authentication in selenium2 chrome driver? I am facing an issue in my project where chrome "Authentication required" popup is coming which is blocking webdriver to continue navigation. Please find the attached screenshot for the same. I am using following code to instantiate chrome driver,
private WebDriver driver;
@Override
protected void setUp() throws Exception {
super.setUp();
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
driver = new ChromeDriver();
}
@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
super.tearDown();
}
你能帮忙吗-
谢谢,
推荐答案
我在同一个问题上苦苦挣扎了一个多小时,最后@kenorb 的解决方案救了我.简而言之,您需要添加一个为您进行身份验证的浏览器扩展程序(因为 Selenium 本身无法做到这一点!).
I've struggled with the same problem over an hour and finally @kenorb's solution rescued me. To be short you need to add a browser extension that does the authentication for you (since Selenium itself can't do that!).
这是 Chrome 和 Python 的工作原理:
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 添加为扩展名
Add the created proxy.zip as an extension
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
这篇关于如何使用 chrome 驱动程序使用 Java 覆盖 selenium2 中的基本身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 chrome 驱动程序使用 Java 覆盖 selenium2 中的基本身份验证?
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01