Handling Basic Authentication Popup with Selenium WebDriver and DevTools API

Basic Authentication Popup Automation

Osanda Deshan Nimalarathna
Test Automation Master
2 min readMay 16, 2021

--

HTTP Basic Authentication

In the context of an HTTP transaction, Basic Access Authentication is a method for an HTTP user agent (e.g. a web browser) to provide a username and password when making a request. In basic HTTP authentication, a request contains a header field in the form of Authorization: Basic <credentials>, where credentials is the Base64 encoding of ID and password joined by a single colon :.

Example: https://the-internet.herokuapp.com/basic_auth

Basic Authentication Popup

Why can‘t we automate Basic Authentication Popup using Selenium?

Selenium language bindings communicates with WebDriver API. WebDriver API communicates to the browser via browser vendor drivers (Ex:- chromedriver, geckodriver).

This entire process is designed to communicate only with HTML elements and not any elements outside the browser.

Here, the Basic Authentication popup is a window which is outside the browser context.

WebDriver API communication to the browser

Older ways to handle the Basic Authentication popup

  1. Passing username and password in the URL itself.
    https://username:password@your_website_url
  2. Using Robot class with Keyboard key events.
  3. Using AutoIT.

Are you one such person who tried all above and bored with their outputs? If yes, then you should try this solution now. Yes, I am pretty sure that you will love it.

Handling the Basic Authentication popup using Selenium 4 and Chrome Dev Tools

  1. Update your Selenium dependency in pom.xml with below.

2. Add following code snippet, before you navigate to the application URL.

That is it. Now you are done with the code. If have any confusions on above code, below is the full code to handle Basic Authentication popup.

To understand how this works, first you need to deep dive the chrome developer tools API documentation. https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setExtraHTTPHeaders

Happy Automation !!!

--

--

Osanda Deshan Nimalarathna
Test Automation Master

Founder of MaxSoft | RPA Solution Architect | Open-source Contributor | Automation Framework Developer | Technical Specialist