Pass Website Basic Authentication Use Selenium WebDriver

Authentication is normally a technology which can make your application more secure. It validate user credentials at the server side first then user can continue to use the application. Basic authentication is used in web applications. It use a browser window to collect user credentials. This can make your website more secure. And block unauthorized users from using your website. This can reduce invalid website traffic also.



If a website use basic authentication, then when you browse a webpage, a window will prompt to gather your username and password. This window is a Browser dialog rather than a JavaScript popup. So it is difficult to use selenium’s sendKeys method  to input credentials.


The solution is also simple, you can just build your page url like following.


Below is java code example

/* First build the page url with the username and password for the authentication.*/
String pageUrl="http://admin:[email protected]";
/* Then use webdriver to get above url. */

Code example

For Chrome Use Selenium Webdriver

 public void testChromeBasicAuth() {
 /* First set the Chrome executable file path. */
 System.setProperty("", "C:/WorkSpace/");
 /* Then create the ChromeDriver object. */
 WebDriver chromeDriver = new ChromeDriver();
 /* Maximize the Chrome browser window. */
 /* Construct page url to pass the website basic authentication. */
 String pageUrl = "http://admin:[email protected]";
 /* Browse the page url above. */
 /* Check whether pass the authentication or not. */
 String text = chromeDriver.findElement(By.className("home")).getText();
 /* If not pass then throw an assertion. */
 Assert.assertTrue("Basic Authentication for this web site failed" , text.contains("yahoo home"));

For Firefox Use Selenium Webdriver

 public void testFirefoxBasicAuth() {

/* Create Firefox driver object and maximize browser window. */
 WebDriver ffDriver = new FirefoxDriver();
 /* Browse the basic authentication web page url. */
 ffDriver.get("http://admin:[email protected]");
 /* Check whether pass the basic authentication or not. */
 String text = ffDriver.findElement(By.className("home")).getText();
 /* Throw assertion when fail. */
 Assert.assertTrue("Basic Authentication for this web site failed" , text.contains("yahoo home"));


For Internet Explorer Use Selenium Webdriver

First you need use another automation test tool “AutoIt” to implement input username and password in the basic authentication popup window. Save following AutoIt script in a file passBasicAuthentication.au3 and compile it to generate the executable file as C:/passBasicAuthentication.exe

; Activate the popup window
WinWaitActive("Windows Security") 
; Input admin in the username text field.
; Click Tab key to move to password text field.
;Input admin in the password text field
;Click Enter key to submit

Below is the java code for the selenium testing script that will use Internet Explorer to run the test. In the code, it will call the AutoIt executable file to pass the website basic authentication.

 public void testIEBasicAuth() {
 /* Create the internet explorer DesiredCapabilities object. */
 DesiredCapabilities itCaps = DesiredCapabilities.internetExplorer();
 /* Set internet explorer default page to an empty page. */
 itCaps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "");

/* Set the internet explorer executeable file path. */
 System.setProperty("", "C:/WorkSpace/");
 /* Create the internet explorer driver object and maximize the browser window. */
 WebDriver ieDriver=new InternetExplorerDriver(itCaps); 
 /* Browse the yahoo home page. */
 try {
 /* Call the AutoIt test script to pass the web site basic authentication. */
 } catch (Exception ex) {

1 thought on “Pass Website Basic Authentication Use Selenium WebDriver”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.