19

Does anyone know about handling Browser Authentication using Selenium or any other tool during automation?

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Harshavardhan Konakanchi
  • 4,238
  • 6
  • 36
  • 54
  • possible duplicate of [How to handle authentication popup with Selenium Webdriver](http://stackoverflow.com/questions/24304752/how-to-handle-authentication-popup-with-selenium-webdriver) – Louis May 06 '15 at 16:24
  • The relative dates of the questions is one factor but not the most important one. The relative quality of the two set of question and related answers is much more important. The accepted answer here starts with "this is outdated , go see that other answer". You can do a search on SO's Meta for "[duplicate-questions] newer" to read the various posts that cover this topic. See [this](http://meta.stackoverflow.com/questions/251938/should-i-flag-a-question-as-duplicate-if-it-has-received-better-answers) question in particular. – Louis May 07 '15 at 10:20

7 Answers7

22

EDIT in 2015:

This answer is outdated. WebDriver nowadays supports authentication! See How to handle authentication popup with Selenium WebDriver using Java


Original answer:

This is not handled very well by Selenium.


You can try using http://username:password@example.com/yourpage

instead of just http://example.com/yourpage

However, as far as I know, Firefox will still pop up a browser dialog requesting a confirmation.


You can try Robot if you're using Java (or any similar tool like AutoIt).


You could use driver.manage().addCookie() if you're using WebDriver.


Or a custom FirefoxProfile that has already passed the authentication once.

Community
  • 1
  • 1
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • Out of curiosity, which path did you take in the end, what was your solution? – Petr Janeček May 15 '12 at 11:59
  • You can try using http://username:password@example.com/yourpage instead of just http://example.com/yourpage – Harshavardhan Konakanchi May 15 '12 at 15:45
  • This posting contains some very relevant information to bypass NTLM browser authentication using Selenium: http://stackoverflow.com/questions/3021602/http-basic-auth-via-url-in-firefox-does-not-work – Justin Sep 30 '13 at 14:43
  • What will solution 3 do? the one with cookies? – Tatarin Jan 16 '15 at 16:16
  • 1
    @Timur Sorry, this answer is old, WebDriver nowadays supports authentication! See http://stackoverflow.com/questions/24304752/how-to-handle-authentication-popup-with-selenium-webdriver – Petr Janeček Jan 16 '15 at 16:30
  • Yes. See that's what's used in the link I put into my last comment,. – Petr Janeček Jan 16 '15 at 16:36
  • I successfully used the FirefoxProfile mentioned above. See https://sqa.stackexchange.com/questions/2277/using-selenium-webdriver-with-windows-authentication for an example. – jsf80238 Dec 31 '20 at 21:34
7

I spent days on this - literally. Trying to get past browser level authentication within my company network to hit an application. The solution was to use the 'unsername:password@' component within the URL, BUT to add a forward slash at the end of the login URL.

So total login URL looks like this (note the '/' after yourpage):

http://username:password@example.com/yourpage/

Works with Watir, Capybara and Selenium Webdriver.

fra_jo
  • 113
  • 1
  • 5
1

All the hacks via auto-it, sikuli, etc. just wasting your time when you'll run it in your CI solution, using several browser types / OS / Version / Resolutions etc.

The way to do it correctly is to identify the authentication actual method and perform a login using Rest protocol for instance.

I used it to get the JSESIONID cookie and insert it to the selenium driver. hint on that: go to a non-exiting url of the domian first, then set the cookie, then go to the required url - you are logged-in.

use: rest client authentication to get the JSESSION ID

and With this information:

browser().navigate(foo.getUrl()+"non-exiting-url");

//the information got from the rest client login:
Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure, isHttpOnly);

try {
    driver.manage().addCookie(cookie);
} catch (Exception e) {
    System.out.println(e.toString());
}

browser().navigate(foo.getUrl());
alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
1

Everything I have read on the Web didn't help me. So before making a request, like this:

driver.get(url);

you have to run a new thread like this:

RunScript runScript = new RunScript();
runScript.start();

In this case you are free to input login and password on another thread of follwing class

public class RunScript extends Thread {

@Override
public void run() {
    try {
        File file = new File("D:\\jacob-1.18-x86.dll");
        System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
        AutoItX autoIt = new AutoItX();
        Thread.sleep(2000);
        autoIt.winActivate("yourWindowName", "");
        autoIt.winWaitActive("yourWindowName");
        if (autoIt.winExists("yourWindowName")) {
            autoIt.send("username{TAB}", false);
            autoIt.send("password{Enter}", false);
            }
        }
    } catch (InterruptedException ex) {
        //
    }
}
}
alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
Roman
  • 11
  • 2
0

you can use auto IT script to handle this problem

WinWaitActive("[CLASS:Chrome_WidgetWin_1]", "", time)
Send("user")
Send("{TAB}")
Send("pass")
Send("{ENTER}")
0

with Chrome 70 and other versions :

http://username:password@example.com/yourpage
-1

You can use Java Robot class with Selenium 2 /Selenium WebDriver using Firefox

WebDriver driver = new FirefoxDriver();
    driver.get("http://localhost:9990");

    WebElement myDynamicElement = driver.findElement(By.id("app"));

    Alert alert = driver.switchTo().alert();


    try {
        Robot robot = new Robot();
        alert.sendKeys("username");

        robot.keyPress(KeyEvent.VK_TAB);//go to password feild

        robot.keyPress(KeyEvent.VK_P);
        robot.keyPress(KeyEvent.VK_A);
        robot.keyPress(KeyEvent.VK_S);
        robot.keyPress(KeyEvent.VK_S);

        robot.keyPress(KeyEvent.VK_ENTER);


        } catch (AWTException e) {
        e.printStackTrace();
        }

    }

Using Selenium with Robot
http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Alex Punnen
  • 5,287
  • 3
  • 59
  • 71