How can we handle authentication popup in Selenium WebDriver using Java

+2 votes
I am trying to handle the authentication popup using this code below:

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.http.phishy-userpass-length", 255);
profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "x.x.x.x");
driver = new FirefoxDriver(profile);
baseUrl="http://" + login + ":" + password + "@" + url;
driver.get(baseUrl + "/");

On executing the test, page prompts with authentication popup and keeps loading till I click on a cancel button. At point, I am able to access the next page meaning that the authentication is a success..But inspite of that, the authentication popup remains displayed.
Apr 17, 2018 in Selenium by kappa3010
• 2,020 points
8,483 views
Hi

how to use this option in chrome browser

kindly reply soon

Thanks

Praveenraj

Hey @Praveen Raj. 

Handling Authentication Popup is simpler for chrome browser.

You can directly pass the username and password while requesting the URL.

Usually, you do: driver.get("http://www.websitename.com/");

But to handle authentication popups, you have to do this:

driver.get("http://username:password@www.websitename.com");

Hi @Praveen,

With Firefox, the preferences is set using FirefoxProfile
But for Chrome, preferences are set using ChromeOptions

But if you're intention is to anyway disable notifications, then use the below logic"

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
WebDriver driver =new ChromeDriver(options);

Hope the code helps! Keep me posted :)

This will fail when we have @ in username or password

So best approach is this -

WebDriverWait wait = new WebDriverWait(driver, 10);

 Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.authenticateUsing(new UserAndPassword(username, password));
Hi @Nayan, I agree with you that using @ in username or password will throw an error. But the answer posted by @Vardhan is a very good approach to disable the pop-ups.

1 answer to this question.

0 votes

The Alert Method, authenticateUsing() lets us skip the Basic Http Authentication box.

WebDriverWait wait = new WebDriverWait(driver, 10);      
Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword(username, password));

You can probably think of using the Alert method, authenticateUsing(). This lets you skip authentication box. Try this:

WebDriverWait wait = new WebDriverWait(driver, 10);      
Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword(username, password));
answered Apr 18, 2018 by king_kenny
• 3,650 points

Related Questions In Selenium

0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,271 views
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
4,945 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
925 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,108 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
845 views
+9 votes
15 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,270 points
69,844 views
+4 votes
2 answers

Can we get the HTTP Response Code in Selenium with Java?

It is indeed possible to get http ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,650 points
14,082 views