简体   繁体   中英

Handling Windows authentication with Selenium Webdriver

Selenium Webdriver based test in C# must login with windows authentication.

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.

I believe the windows authentication dialog is provided by the browser, but I haven't found any source for the dialog.

Using Selenium (not AutoIt or other similar tools) how do I pass a username and password into the windows authentication dialog? The solution must be based on Selenium code, not add-ins, I have no access to the browsers as the are in the cloud.

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.

You can NOT auth through this window with Selenium. Because of this is not browser window, but OS's authentication popup. Selenium have no ability to work with other processes except browsers.

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 . 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. 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. In case of service have short cookies life time, you can use "AutoAuth" plugin for your browser additionally in your profile from step 2.

upd: as I know there is some new way for selenium 3.0, you need to search more. 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. 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.

当通过 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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