Tra*_*ham 11 java authentication selenium google-chrome selenium-webdriver
我正在尝试在我的一个新的Webdriver脚本中处理身份验证弹出窗口.我有一个适用于IE的工作解决方案,但我正在努力使用Chrome.IE就像遵循[本页]上的建议一样简单:如何使用Java处理Selenium WebDriver的身份验证弹出窗口.该线程并没有为Chrome提供一个很好的解决方案,尽管有几位评论员指出,该解决方案不适用于Chrome.问题是,当您尝试在Chrome上执行以下代码时,登录弹出窗口不是警报.
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
Run Code Online (Sandbox Code Playgroud)
它不是一个windows level()认证弹出窗口,网页只是受密码保护.我知道Stack Overflow上有这个问题的其他几个例子,但我最近没有看到比2岁更多.我希望2017年有更好的解决方案.提前感谢.
*edit Chrome 不再支持此功能。
这不是可以通过在地址前面加上用户名和密码来处理的“受限”弹出窗口吗?
而不是driver.get("http://www.example.com/");去driver.get("http://username:password@www.example.com");。
借助chrome扩展程序,可能对其他人在chrome中解决此问题可能有所帮助。感谢@SubjectiveReality,他给了我这个主意。
如果同一服务器同时执行身份验证和托管应用程序,则将用户名和密码作为URL的一部分(例如https:// username:password@www.mytestsite.com)发送可能会有所帮助。但是,大多数公司应用程序都具有公司范围的身份验证,应用服务器可能会将请求重新路由到身份验证服务器。在这种情况下,无法在URL中传递凭据。
解决方法如下:
{“ name”:“ Webrequest API”,“ version”:“ 1.0”,“ description”:“扩展以处理身份验证窗口”,“ permissions”:[“”,“ webRequest”,“ webRequestBlocking”],“背景” :{“脚本”:[“ webrequest.js”]},“清单版本”:2}
Run Code Online (Sandbox Code Playgroud)chrome.webRequest.onAuthRequired.addListener( function handler(details){ return {'authCredentials': {username: "yourusername", password: "yourpassword"}}; }, {urls:["<all_urls>"]}, ['blocking']);
打开chrome浏览器,转到chrome:// extensions并打开开发人员模式
单击“打包扩展名”,选择根目录作为“扩展名”并打包扩展名。它应该创建一个扩展名为“ .crx”的文件
Run Code Online (Sandbox Code Playgroud)options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox");
测试愉快!
参考文献:
http://www.adambarth.com/experimental/crx/docs/webRequest.html#apiReference https://developer.chrome.com/extensions/webRequest#event-onAuthRequired chrome.webRequest.onAuthRequired侦听器 https:// gist。 github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
@Bhuvanesh Mani 提供的解决方案的更新答案
清单文件
{
"name": "Webrequest API",
"version": "1.0",
"description": "Extension to handle Authentication window",
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"background": {
"scripts": [
"webrequest.js"
]
},
"manifest_version": 2
}
Run Code Online (Sandbox Code Playgroud)
网页请求.js
chrome.webRequest.onAuthRequired.addListener(function(details){
console.log("chrome.webRequest.onAuthRequired event has fired");
return {
authCredentials: {username: "yourusername", password: "yourpassword"}
};
},
{urls:["<all_urls>"]},
['blocking']);
Run Code Online (Sandbox Code Playgroud)
不确定为什么需要“--no sandbox”选项,但对我来说这不是必需的。
我确实需要执行注册表编辑以允许添加扩展,因为这已被公司 AD 策略禁用:
*例如-。与原始答案的唯一区别是 url 也需要出现在权限中。
额外的
,如果你想看到控制台输出,打开开发人员模式,然后单击Inspect views background page在扩展您的扩展屏幕的Chrome
| 归档时间: |
|
| 查看次数: |
20287 次 |
| 最近记录: |