简体   繁体   English

使用 Selenium Webdriver 处理 Windows 身份验证

[英]Handling Windows authentication with Selenium Webdriver

Selenium Webdriver based test in C# must login with windows authentication.在 C# 中基于 Selenium Webdriver 的测试必须使用 Windows 身份验证登录。

I have tried a couple of approaches:我尝试了几种方法:

  _Driver.SwitchTo().Alert();
  _Driver.FindElement(By.Id("UserName")).SendKeys("LynnTest");
  _Driver.FindElement(By.Id("Password")).SendKeys("Welcome1!");
  _Driver.SwitchTo().Alert().Accept();
  _Driver.SwitchTo().DefaultContent();

and

  IAlert alert = _Driver.SwitchTo().Alert();
  alert.SendKeys("LynnTest\\t");
  alert.SendKeys("Welcome1!");
  _Driver.SwitchTo().Alert().Accept();
  _Driver.SwitchTo().DefaultContent();

Neither one is successful.没有一个是成功的。 I don't get the windows authentication dialog on my local system so I can't view the source to determine how to locate the username and password with the Selenium By method.我的本地系统上没有 Windows 身份验证对话框,因此我无法查看源代码以确定如何使用 Selenium By 方法定位用户名和密码。

I believe the windows authentication dialog is provided by the browser, but I haven't found any source for the dialog.我相信 Windows 身份验证对话框是由浏览器提供的,但我没有找到该对话框的任何来源。

Using Selenium (not AutoIt or other similar tools) how do I pass a username and password into the windows authentication dialog?使用 Selenium(不是 AutoIt 或其他类似工具)如何将用户名和密码传递到 Windows 身份验证对话框中? The solution must be based on Selenium code, not add-ins, I have no access to the browsers as the are in the cloud.解决方案必须基于 Selenium 代码,而不是加载项,我无法访问云中的浏览器。

Note: passing the username and password in the URL does not work, as I understand because the dialog is not generated by html on the page.注意:在 URL 中传递用户名和密码不起作用,据我了解,因为该对话框不是由页面上的 html 生成的。

You can NOT auth through this window with Selenium.您无法使用 Selenium 通过此窗口进行身份验证。 Because of this is not browser window, but OS's authentication popup.因为这不是浏览器窗口,而是操作系统的身份验证弹出窗口。 Selenium have no ability to work with other processes except browsers.除了浏览器之外,Selenium 无法与其他进程一起工作。

But you can auth WITHOUT getting this window.但是您可以在不获得此窗口的情况下进行身份验证。

There is 2 ways to miss it:有两种方法可以错过它:

  1. Change URL with authentication to: http://user:password@www.yourserveradress.com .将带有身份验证的 URL 更改为: http://user:password@www.yourserveradress.com 。 This will work in part of situations and will authenticate you to needed service.这将在部分情况下起作用,并将验证您所需的服务。 In another case you can use way 2. [too old way, doesnt work now]在另一种情况下,您可以使用方式 2。 [太旧的方式,现在不起作用]

  2. You need to create a new profile in browser, pass authentication to needed service and remember password/authentication.您需要在浏览器中创建一个新的配置文件,将身份验证传递给所需的服务并记住密码/身份验证。 And, ofc, use created browser profile in your tests.此外,在您的测试中使用创建的浏览器配置文件。

    2.1. 2.1. In case of service have short cookies life time, you can use "AutoAuth" plugin for your browser additionally in your profile from step 2.如果服务的 cookie 生命周期较短,您可以从步骤 2 开始,在您的个人资料中额外为您的浏览器使用“AutoAuth”插件。

upd: as I know there is some new way for selenium 3.0, you need to search more. upd:据我所知,selenium 3.0 有一些新方法,您需要进行更多搜索。 This is too old answer.这是太旧的答案。 please ping me if you will wind better way.如果你能以更好的方式风,请给我打电话。


  1. You can write own realization of login through the login popup with "White" framework.您可以使用“White”框架通过登录弹出窗口编写自己的登录实现。 On needed step you can check all shown OS's windows for "correct" title.在需要的步骤中,您可以检查所有显示的操作系统窗口的“正确”标题。 And in case of such title exist, get window's handle.如果存在此类标题,请获取窗口的句柄。 And to do login using "White".并使用“白色”进行登录。

  2. You can use the following project https://github.com/ukushu/DialogCapabilities as sample and write your own realisation without using "White" framework.您可以使用以下项目https://github.com/ukushu/DialogCapabilities作为示例,并在不使用“White”框架的情况下编写自己的实现。

当通过 url 传递凭据不起作用时,您可以通过使用外部软件“AutoIt V3”来做到这一点。这与 Windows 身份验证配合得很好。

In the end I used a workaround, navigating first to the authentication page, authenticating and then redirecting to the page to be tested as an authenticated user.最后,我使用了一种解决方法,首先导航到身份验证页面,进行身份验证,然后重定向到要作为经过身份验证的用户进行测试的页面。 This approach uses pages internal authentication page which can be scripted to.这种方法使用可以编写脚本的页面内部身份验证页面。

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

相关问题 如何使用 Selenium Webdriver 和 Internet Explorer 10 处理 Windows 身份验证? - How to handle Windows Authentication with Selenium Webdriver and Internet Explorer 10? 使用 Selenium webdriver 处理 Select2 - Handling Select2 with Selenium webdriver 如何在“ Windows身份验证对话框”上应用自动化并使用c#,Selenium Webdriver登录到Web应用程序 - How to apply automation on “Windows authentication dialog” and login into the web application using c# , selenium webdriver 使用具有 Windows 身份验证的 Selenium Webdriver(在 Chrome 中)C#/ASP.Net - Using Selenium Webdriver with Windows Authentication (in Chrome) C#/ASP.Net Web 表处理 Selenium Webdriver c# - Web Table handling Selenium Webdriver c# Selenium WebDriver测试登录验证/身份验证 - Selenium WebDriver testing login verification/authentication 警报处理 - Selenium WebDriver / Selenium RC 2.18.0 - 例外 - Alert Handling - Selenium WebDriver/ Selenium RC 2.18.0 - Exception 为什么Selenium Webdriver冻结Windows窗体应用程序? - Why Selenium Webdriver freezes the Windows Form Appliaction? 通过C#处理Selenium Webdriver中动态生成的元素 - Handling dynamically generated elements in selenium webdriver via C# C# Selenium WebDriver FireFox Profile - 使用代理和身份验证 - C# Selenium WebDriver FireFox Profile - using proxy with Authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM