简体   繁体   中英

How to handle authentication required pop up in next tab in selenium webdriver using java?

In Chrome, clicking a certain link opens a new tab with an "Authentication Required" popup. As you can see, it has 'User name' and 'password' with 'Log In' and 'Cancel' buttons.

With Java code, how can I enter values (user name and password) in this popup? Because I can't get the locator of username and password, also the alert method is not working here.

在此处输入图片说明

The code I tried:

public static void switchToalert() {
    driver.switchTo().alert();
    // Selenium-WebDriver Java Code for entering Username & Password as // below: 
    driver.findElement(By.id("Username")).sendKeys("userName");
    driver.findElement(By.id("Password")).sendKeys("myPassword");
    driver.switchTo().alert().accept();
    driver.switchTo().defaultContent();
}  

Thanks Govind

Handle Windows Authentication using Selenium Webdriver

You can provide credentials in URL itself it means we will add username and password in URL so while running script it will bypass the same

http://username:password@yourWebURL
##example##

http:
//GuravPraveen:Passwrd123@YourURL


driver.get("http://GuravPraveen:Passwrd123@www.xyz.com/signin");

The approach I've used very successfully is to set up an embedded Browsermob proxy server (in Java code) and register a RequestInterceptor to intercept all incoming requests (that match the host / URL pattern in question).

When you have a request that would otherwise need Basic auth, add an Authorization HTTP header with the credentials required ('Basic ' + the Base64-encoded 'user:pass' string. So for 'foo:bar' you'd set the value Basic Zm9vOmJhcg== )

Start the server, set it as a web proxy for Selenium traffic , and when a request is made that requires authentication, the proxy will add the header, the browser will see it, verify the credentials, and not need to pop up the dialog.

You won't need to deal with the dialog at all. Another benefit is that because it's a pure HTTP solution, it works the same across all browsers and operating systems.

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