繁体   English   中英

如何使用 Java 使用 Selenium WebDriver 在 Chrome 中处理身份验证弹出窗口

[英]How to handle authentication popup in Chrome with Selenium WebDriver using Java

我正在尝试在我的一个新 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**));

它不是 Windows 级别 () 身份验证弹出窗口,网页只是受密码保护。 我知道 Stack Overflow 上还有其他几个这个问题的实例,但我最近没有看到超过 2 年的实例。 我希望 2017 年现在有更好的解决方案。提前致谢。

在 chrome 扩展的帮助下,可能有助于其他人在 chrome 中解决这个问题。 感谢@SubjectiveReality 给了我这个想法。

如果同一服务器同时执行身份验证并托管应用程序,则将用户名和密码作为 url 的一部分(如https://username:password@www.mytestsite.com)发送可能会有所帮助。 然而,大多数企业应用程序都有公司范围的身份验证,应用程序服务器可能会将请求重新路由到身份验证服务器。 在这种情况下,在 URL 中传递凭据将不起作用。

这是解决方案:

步骤 1:创建 chrome 扩展

  1. 创建一个名为“扩展”的文件夹
  2. 在“extension”文件夹中创建一个名为“manifest.json”的文件。 将以下代码复制到文件中并保存。

{“名称”:“Webrequest API”,“版本”:“1.0”,“描述”:“处理身份验证窗口的扩展”,“权限”:[“”,“webRequest”,“webRequestBlocking”],“背景” :{“脚本”:[“webrequest.js”]},“manifest_version”:2}

  1. 在“扩展”文件夹中创建一个名为“webrequest.js”的文件,并将下面的代码粘贴到文件中并保存。
 chrome.webRequest.onAuthRequired.addListener( function handler(details){ return {'authCredentials': {username: "yourusername", password: "yourpassword"}}; }, {urls:["<all_urls>"]}, ['blocking']);
  1. 打开chrome浏览器,进入chrome://extensions,开启开发者模式

  2. 单击“打包扩展”,选择根目录为“扩展”并打包扩展。 它应该创建一个扩展名为“.crx”的文件

步骤 2:将扩展添加到您的测试自动化框架中

  1. 将 .crx 文件复制到您的框架中
  2. 配置您的 webdriver 创建以加载扩展,如
options.addExtensions(new File("path/to/extension.crx")); options.addArguments("--no-sandbox");
  1. 调用您的 webdriver 和应用程序 URL
  2. 您永远不会看到身份验证弹出窗口显示为由上述扩展程序处理

测试愉快!

参考:

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

*edit Chrome 不再支持此功能。

这不是可以通过在地址前面加上用户名和密码来处理的“受限”弹出窗口吗?

而不是driver.get("http://www.example.com/"); driver.get("http://username:password@www.example.com"); .

@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
    }

网页请求.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']);

不确定为什么需要“--no sandbox”选项,但对我来说这不是必需的。

我确实需要执行注册表编辑以允许添加扩展,因为这已被公司 AD 策略禁用:

  • 以管理员身份打开 regedit.exe
  • 导航到 Computer\\HKEY_USERS\\
  • 将现在设置为*的 REG_SZ 值更改为例如-
  • 重新启动要在其中添加扩展的 chrome 实例。

与原始答案的唯一区别是 url 也需要出现在权限中。

额外的
如果您想查看控制台输出,请打开开发人员模式并在 Chrome 的扩展程序屏幕中单击扩展程序的Inspect views background page

我知道您的情况是 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();

然后我使用一个测试用户帐户作为 user_name 和密码的值。

Selenium 4支持使用 Basic 和 Digest auth 进行身份验证。 它使用 CDP,目前仅支持基于铬的浏览器

Java示例:

Webdriver driver = new ChromeDriver();

((HasAuthentication) driver).register(UsernameAndPassword.of("username", "pass"));

driver.get("http://sitewithauth");

注意:在 Alpha-7 中存在一个错误,它会同时发送用户名/密码的用户名。 需要等待 selenium 版本的下一个版本,因为主干中有修复https://github.com/SeleniumHQ/selenium/commit/4917444886ba16a033a81a2a9676c9267c472894

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM