Search code examples
c#seleniumselenium-webdriverwindows-authentication

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.


Solution

  • 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.