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 18, 2018 in Selenium by kappa3010
• 2,090 points
29,070 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.

3 answers 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));

I hope this helps!

Enroll for the Selenium Course to learn more about the Selenium Webdriver.

Thanks!

answered Apr 18, 2018 by king_kenny
• 3,710 points
+1 vote

You can use below code for selenium python
Python has an inbuilt package called Autoit. So,
firstly install pyautoit and pyautogui package.

Pip install pyautoit
Pip install pyautogui

Use below code:

autoit.win_wait_active([CLASS:Chrome_WidgetWin_1]", 5)
autoit send("username")
pyautogui.press("tab")
autoit.send("password")
pyautogui.press("enter")

Above code works with every chrome browser,. Selenium and python version.

Use autoit tool and find class name using finder tool.

answered Jun 22, 2020 by TEJAS KIRAN RATHI
+1 vote
1) By passing user credentials in URL.

String URL = "http://" + rajkumar + ":" + myPassword + "@" + www.softwaretestingmaterial.com;
driver.get(URL);
Alert alert = driver.switchTo().alert();
alert.accept();

2) AutoIT

3) selenium code

4) way also we have
answered Aug 31, 2020 by Sri
• 3,190 points

Related Questions In Selenium

+1 vote
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,500 points
8,357 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 6, 2018 in Selenium by nsv999
• 5,500 points
10,556 views
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

How to handle browser popup in Selenium using Java?

Generally, we scrap the data from websites ...READ MORE

Jun 7, 2019 in Selenium by Vaishnavi
• 1,180 points
1,723 views
0 votes
1 answer

How can I handle Javascript Alert popup using Ruby Selenium webdriver?

Hey Usman, you can use driver.switch_to.alert method ...READ MORE

answered Aug 27, 2019 in Selenium by Abha
• 28,140 points
2,305 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP