Testing Pages with Basic Authentication using Selenium

Arun
5 min read
Featured Image

Sometimes pages are secured using Basic HTTP Authentication (Basic Auth) to prevent unauthorized access. A system level pop-up prompts to enter username and password and Selenium will not be able to access it.

Solution

A Chrome Add-on can be used to insert the credentials when Selenium access the Page

  1. Inspect (F12) the request in Network Tab of Developer tools
  2. Look fo the Authorization code in the  Request Headers like below
    GET /basic-auth/user/passwd HTTP/1.1
    Host: httpbin.org
    Connection: keep-alive
    Authorization: Basic dXNlcjpwYXNzd2Q=
  3. Clone the Github Repo
    git clone https://github.com/arunelias/Selenium-Chrome-Addon.git
  4. Edit the background.js and insert the Authorization code in the line no: 4
    details.requestHeaders.push({name: "Authorization",value: "Basic dXNlcjpwYXNzd2Q="});
  5. Modify the Selenium code and include the folder where the background.js file exists

Example:

from selenium import webdriver
# Set Chrome options. options = webdriver.ChromeOptions()
# Modify the path of the folder
options.add_argument('--load-extension=path/to/the/extension/folder')
driver = webdriver.Chrome(chrome_options=options)
driver.get('https://httpbin.org/basic-auth/user/passwd')
# Your Code goes here...
driver.quit()

Using Selenium with Python in Eclipse

Arun Arun
20 min read