Java – how do I switch to the authentication pop-up window and enter credentials?

After opening the application URL, the user will be redirected to the login page with a login button

driver.get("abc.com")

Now, when the user clicks the login button, the URL changes in the same window, indicating that it becomes XYZ COM and displays an authentication pop-up window for login purposes, similar to the image shown in the figure below

To enter a user name and password in the authentication window, I tried the following code

shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("username")
time.sleep(1)
shell.Sendkeys("{TAB}")
time.sleep(1)
shell.Sendkeys("password") 
time.sleep(1)
shell.Sendkeys("{ENTER}")
time.sleep(1)

It's useless Then I try to use the above code to directly open the windows authentication pop-up window (copy the URL by clicking the login button), and it works normally

driver.get("xyz.com")//instead of abc.com my application URL

I'm a little confused If I open my application URL ABC COM, click the login button and use AutoIT, which does not enter credentials However, if I send the window authentication URL XYZ directly Com instead of APP URL ABC COM and use AutoIT, it can work

Anyone can suggest what I lack here? I also try to switch the window after clicking the login button, consider its new URL, and then automatically command, but it still doesn't work properly

driver.switch_to_window(driver.window_handles[1])

Any ideas?

Note: I noticed that by clicking the login button, the window will load indefinitely & the cursor is active on the text field of the user name of the windows authentication popup In addition, once the windows authentication window appears, the selenium command does not work, and there is no automatic command

Solution

This dialog is considered an alert by selenium In c#, enter the code for Firefox test credentials as follows:

// Inputs id with permissions into the Windows Authentication @R_733_2419@
var alert = driver.SwitchTo().Alert();
alert.SendKeys( @"username" + Keys.Tab + 
                @"password" + Keys.Tab);
alert.Accept();

The first line tells the driver to check the open alert (dialog box)

The second line sends the user name and password to the alert

The third line then sends these credentials and assumes that they are valid, and the test will continue

Assuming you are testing with Firefox, you do not need to use an additional framework to handle this authentication box

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>