Handling Browser Authentication using Selenium

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


Solution 1:

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:[email protected]/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.

Solution 2:

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:[email protected]/yourpage/

Works with Watir, Capybara and Selenium Webdriver.

Solution 3:

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) {
        //
    }
}
}