简体   繁体   中英

Complete Internet Explorer Authentication Dialog with Selenium

I am using Selenium to simulate a user to automate some legacy software. The software works only with IE6 (I'm using IE11 in compatibility mode) and is a bit crap.

There is a point in the software where the Windows Security dialog appears. This requires credentials before the user/simulator can proceed.

I'm using IAlert.SetAuthenticationCredentials to try and populate the dialog but this doesn't seem to work. To move on from this, I can enter the details manually, but then Selenium seems to thing the main browser window has been closed:

Currently focused window has been closed.

The WindowHandles collection at this point is empty, but the browser window is still open, and has rendered the correct page.

What's going on here?

屏幕截图

UPDATE

The answers provided are suggestions on how to handle the dialog. I'm wondering why Selenium thinks the browser window is closed when in fact it is still there.

It is not possible to interract with native windows via selenium . The way to deal with your issue is for example to use analogue of Robot in Java. Since you are using C# there is a simulator here https://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library .

Example code would be like following:

// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);

// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);

There are also Mouse simulators, so with this framework it will be possible to enter the required values in window and accept it.

Try to switch to that alert by,

var alert = driver.SwitchTo().Alert();
alert.SetAuthenticationCredentials("Username", "Pwd");
alert.Accept();

I have tested it and it works for IE11, selenium v3.1.0

Ref: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IAlert_SetAuthenticationCredentials.htm

Suggesstion 1-Go to internet explorer settings->security settings-> user authentication-> select automatic login with current username and password. 在此处输入图片说明

Suggesstion 2- if your application has access to it's API, then login via API, get the authentication token and set the auth.token in browser cookie.

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