问题
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.
回答1:
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:
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]
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.
- 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:
When passing credentials through url is not working, you can do this by is using an external software "AutoIt V3" This works well with the windows authentication.
回答3:
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.
来源:https://stackoverflow.com/questions/30247051/handling-windows-authentication-with-selenium-webdriver