55

I'm trying to handle authentication popup using the 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 + "/");

When I execute the test, the page shows the authentication popup and still loading for a until I click cancel button. A that moment, I can access to the next page ,this mean that the authentication success but still always show the authentication popup

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Imen CHOK
  • 930
  • 1
  • 12
  • 20
  • 1
    Check this to handle alerts/popups http://stackoverflow.com/questions/17066382/alert-handling-java-webdriver – Sriram Jun 19 '14 at 12:06
  • I check many alternative but it didn't work – Imen CHOK Jun 19 '14 at 13:24
  • Which alternatives, please be more specific. – Stephen B. Jun 19 '14 at 14:51
  • I tried to authenticate using http://login:password@url with and without Firefox Profile/// Also, I tried to use ''____String window1 = driver.getWindowHandle(); driver.findElement(By.cssSelector("input")).sendKeys(login);_____''/// And i tried ti access to popup with driver.switchTo().alert()/// And any think of those work prperly // always the test stuck in loading address with popup in screen – Imen CHOK Jun 19 '14 at 14:56
  • This question should be updated to note the type of authentication pop up that is being displayed. As the marked solution does not work for the browser authentication required pop up. – Acludia Aug 11 '17 at 15:15
  • Now with selenium 4, it's very easy. Please check my answer here https://stackoverflow.com/a/67321556/7604647 – Pavel Apr 29 '21 at 16:40

8 Answers8

40

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

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

As of Selenium 3.4 it is still in beta

Right now implementation is only done for InternetExplorerDriver

Madhan
  • 5,750
  • 4
  • 28
  • 61
Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
36

Don't use firefox profile and try below code:

driver.get("http://UserName:Password@Example.com");

If you're implementing it in IE browser, there are certain things which you need to do.

In case your authentication server requires username with domain like "domainuser" you need to add double slash / to the url:

//localdomain\user:password@example.com
AskYous
  • 4,332
  • 9
  • 46
  • 82
Raghav Arora
  • 390
  • 4
  • 9
8

Try following solution and let me know in case of any issues:

driver.get('https://example.com/')
driver.switchTo().alert().sendKeys("username" + Keys.TAB + "password");
driver.switchTo().alert().accept();

This is working fine for me

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user6939
  • 363
  • 1
  • 5
  • 13
  • 1
    That's the one that works for me. A bit unreliable when showing the UI (probably due to how i3wm handles focus though) but it works fine in headless. – Gonzague Oct 25 '18 at 09:48
  • 1
    not for me unfortunately. I get : Step failed org.openqa.selenium.NoAlertPresentException: no such alert – Bastien Gallienne Oct 31 '22 at 15:07
7

I faced this issue a number of times in my application.

We can generally handle this with the below 2 approaches.

  1. Pass the username and password in url itself

  2. You can create an AutoIT Script and call script before opening the url.

Please check the below article in which I have mentioned both ways:
Handle Authentication/Login window in Selenium Webdriver

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Mukesh Otwani
  • 291
  • 4
  • 2
  • AutoIt is a primitive shame. Why does Selenium Driver not implement this important feature? – Elmue May 16 '17 at 17:38
  • 2
    I did the exact same approach before and both works great. AutoIT is a very reliable tool if you write your commands correctly, it's very stable. – ibaralf Oct 24 '18 at 22:20
3

This should work for Firefox by using AutoAuth plugin:

FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default");
File ffPluginAutoAuth = new File("D:\\autoauth-2.1-fx+fn.xpi");
firefoxProfile.addExtension(ffPluginAutoAuth);
driver = new FirefoxDriver(firefoxProfile);
djangofan
  • 28,471
  • 61
  • 196
  • 289
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
3

Popular solution is to append username and password in URL, like, http://username:password@website.com. However, if your username or password contains special character, then it may fail. So when you create the URL, make sure you encode those special characters.

String username = URLEncoder.encode(user, StandardCharsets.UTF_8.toString());
String password = URLEncoder.encode(pass, StandardCharsets.UTF_8.toString());
String url = “http://“ + username + “:” + password + “@website.com”;
driver.get(url);
Bhuvanesh Mani
  • 1,394
  • 14
  • 23
3

Selenium 4 supports authenticating using Basic and Digest auth . It's using the CDP and currently only supports chromium-derived browsers

Java Example :

Webdriver driver = new ChromeDriver();

((HasAuthentication) driver).register(UsernameAndPassword.of("username", "pass"));

driver.get("http://sitewithauth");

Note : In Alpha-7 there is bug where it send username for both user/password. Need to wait for next release of selenium version as fix is available in trunk https://github.com/SeleniumHQ/selenium/commit/4917444886ba16a033a81a2a9676c9267c472894

Rahul L
  • 4,249
  • 15
  • 18
2

If you have to deal with NTLM proxy authentication a good alternative is to use a configure a local proxy using CNTLM.

The credentials and domain are configured in /etc/cntlm.conf.

Afterwards you can just use you own proxy that handles all the NTLM stuff.

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:3128");
capabilities.setCapability(CapabilityType.PROXY, proxy);

driver = new ChromeDriver(capabilities);
cjungel
  • 3,701
  • 1
  • 25
  • 19
  • As I have read CNTLM does not work with HTTPS. This makes it completely usless for me. https://sourceforge.net/p/cntlm/support-requests/14. Apart from that CNTLM is a dead project. Bugreports have no answers since 2012. – Elmue May 16 '17 at 18:34