如何使用 Java 使用 Selenium WebDriver 在 Chrome 中处理身份验证弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42114940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to handle authentication popup in Chrome with Selenium WebDriver using Java
提问by Travis Needham
I am trying to handle an authentication pop-up in one of my new Webdriver scripts. I have a working solution for IE, but I am struggling with Chrome. IE was as simple as following the advice on [this page]:How to handle authentication popup with Selenium WebDriver using Java. That thread doesn't show a great solution for Chrome, although several commentors point out, that the solution does not work for Chrome. The problem is, when you try to do the below code on Chrome, the login popup isn't an Alert.
我正在尝试在我的一个新 Webdriver 脚本中处理身份验证弹出窗口。我有一个适用于 IE 的有效解决方案,但我在使用 Chrome 时遇到了困难。IE 就像遵循 [this page] 上的建议一样简单:How to handle authentication popup with Selenium WebDriver using Java。该线程并没有为 Chrome 显示一个很好的解决方案,尽管一些评论者指出,该解决方案不适用于 Chrome。问题是,当您尝试在 Chrome 上执行以下代码时,登录弹出窗口不是警报。
WebDriverWait wait = new WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.authenticateUsing(new UserAndPassword(**username**, **password**));
It's not a windows level () authentication pop-up, the web page is simply password protected. I know there are several other instances of this question on Stack Overflow, but I don't see any more recently than 2 years old. I am hoping there is a better solution now in 2017. Thanks in advance.
它不是 Windows 级别 () 身份验证弹出窗口,网页只是受密码保护。我知道 Stack Overflow 上还有其他几个这个问题的实例,但我最近没有看到超过 2 年的实例。我希望 2017 年现在有更好的解决方案。提前致谢。
采纳答案by Grzegorz Górkiewicz
*edit Chrome no longer supports this.
*edit Chrome 不再支持此功能。
Isn't that a "restricted" pop-up that can be handled by prepending the address with username and password?
这不是可以通过在地址前面加上用户名和密码来处理的“受限”弹出窗口吗?
Instead of driver.get("http://www.example.com/");
go for driver.get("http://username:[email protected]");
.
而不是driver.get("http://www.example.com/");
去driver.get("http://username:[email protected]");
。
回答by joshmcode
I know your situation is Java, but this might be helpful. I was able to get past the alert using this approach in C#:
我知道您的情况是 Java,但这可能会有所帮助。我能够在 C# 中使用这种方法绕过警报:
// I am using a static instance of the driver for this.
Driver.Instance.Navigate().GoToUrl(url);
var alert = Driver.Instance.SwitchTo().Alert();
alert.SendKeys($"{Driver.user_name}" + Keys.Tab + $"{Driver.user_password}" + Keys.Tab);
alert.Accept();
Driver.Instance.SwitchTo().DefaultContent();
I then utilized a test user account as the values of the user_name and password.
然后我使用一个测试用户帐户作为 user_name 和密码的值。
回答by Bhuvanesh Mani
May be helpful for others to solve this problem in chrome with the help of chrome extension. Thanks to @SubjectiveReality who gave me this idea.
在 chrome 扩展的帮助下,可能有助于其他人在 chrome 中解决这个问题。感谢@SubjectiveReality 给了我这个想法。
Sending username and password as part of url like https://username:[email protected]may be helpful if same server performs both authentication and hosts the application. However most corporate applications have firmwide authentications and app server may reroute the request to authentication servers. In such cases, passing credentials in URL wont work.
如果同一服务器同时执行身份验证并托管应用程序,则将用户名和密码作为 url 的一部分(如https://username:[email protected])发送可能会有所帮助。然而,大多数企业应用程序都有公司范围的身份验证,应用程序服务器可能会将请求重新路由到身份验证服务器。在这种情况下,在 URL 中传递凭据将不起作用。
Here is the solution:
这是解决方案:
Step1: Create chrome extension
步骤 1:创建 chrome 扩展
- Create a folder named 'extension'
- Create a file named 'manifest.json' inside 'extension' folder. Copy below code into the file and save it.
- 创建一个名为“扩展”的文件夹
- 在“extension”文件夹中创建一个名为“manifest.json”的文件。将以下代码复制到文件中并保存。
{ "name":"Webrequest API", "version":"1.0", "description":"Extension to handle Authentication window", "permissions":["","webRequest","webRequestBlocking"], "background": { "scripts" : ["webrequest.js"] }, "manifest_version": 2 }
{ "name":"Webrequest API", "version":"1.0", "description":"处理身份验证窗口的扩展", "permissions":["","webRequest","webRequestBlocking"], "background" :{“脚本”:[“webrequest.js”]},“manifest_version”:2}
- Create a file named 'webrequest.js' inside 'extension' folder and copy paste below code into the file and save it.
- 在“扩展”文件夹中创建一个名为“webrequest.js”的文件,并将下面的代码粘贴到文件中并保存。
chrome.webRequest.onAuthRequired.addListener( function handler(details){ return {'authCredentials': {username: "yourusername", password: "yourpassword"}}; }, {urls:["<all_urls>"]}, ['blocking']);
chrome.webRequest.onAuthRequired.addListener( function handler(details){ return {'authCredentials': {username: "yourusername", password: "yourpassword"}}; }, {urls:["<all_urls>"]}, ['blocking']);
Open chrome browser, go to chrome://extensions and turn on developer mode
Click 'Pack Extension', select root directory as 'extension' and pack extension. It should create a file with extension '.crx'
打开chrome浏览器,进入chrome://extensions,开启开发者模式
单击“打包扩展”,选择根目录为“扩展”并打包扩展。它应该创建一个扩展名为“.crx”的文件
Step2: Add extension into your test automation framework
步骤 2:将扩展添加到您的测试自动化框架中
- Copy the .crx file into your framework
- Configure your webdriver creation to load the extension like
- 将 .crx 文件复制到您的框架中
- 配置您的 webdriver 创建以加载扩展,如
options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox");
options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox");
- Invoke your webdriver and application URL
- You wont never see the authentication popup appearing as its handled by above extension
- 调用您的 webdriver 和应用程序 URL
- 您永远不会看到身份验证弹出窗口显示为由上述扩展程序处理
Happy Testing!
测试愉快!
References:
参考:
http://www.adambarth.com/experimental/crx/docs/webRequest.html#apiReferencehttps://developer.chrome.com/extensions/webRequest#event-onAuthRequiredchrome.webRequest.onAuthRequired Listenerhttps://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
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
回答by obiwankoban
Updated answer of the solution provided by @Bhuvanesh Mani
@Bhuvanesh Mani 提供的解决方案的更新答案
manifest.json
清单文件
{
"name": "Webrequest API",
"version": "1.0",
"description": "Extension to handle Authentication window",
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"background": {
"scripts": [
"webrequest.js"
]
},
"manifest_version": 2
}
webrequest.js
网页请求.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']);
Not certain why the '--no sandbox' option is required, but for me this is not required.
不确定为什么需要“--no sandbox”选项,但对我来说这不是必需的。
I did need to perform a registry edit to allow adding extensions since this is disabled with a corporate AD policy:
我确实需要执行注册表编辑以允许添加扩展,因为这已被公司 AD 策略禁用:
- Open regedit.exe as admin
- navigate to Computer\HKEY_USERS\
- Change the REG_SZ value that is now set to
*
to for example-
. - restart the chrome instance in which you want add extensions to.
- 以管理员身份打开 regedit.exe
- 导航到 Computer\HKEY_USERS\
- 更改现在设置为的 REG_SZ 值,
*
例如-
。 - 重新启动要在其中添加扩展的 chrome 实例。
The only difference with the original answer is the url's that also need to be present in the permissions.
与原始答案的唯一区别是 url 也需要出现在权限中。
extra's
if you want to see console output, turn on developer mode and click on the Inspect views background page
for your extension in the extensions screen in Chrome
额外的
,如果你想看到控制台输出,打开开发人员模式,然后单击Inspect views background page
在扩展您的扩展屏幕的Chrome