6

Is it possible to handle "Authentication Required" Pop-up in selenium which having fields as "UserName" and "Password" using Alert.

enter image description here

4
  • 5
    If the authentication pop-up is from HTTP Basic Auth you can send the username and password in the URL: http://username:[email protected]/; see also serverfault.com/questions/371907/… Commented Apr 11, 2017 at 6:45
  • Sure but in which programming language? Commented Apr 14, 2017 at 7:04
  • @BillalBEGUERADJ I want to handle “Authentication Required” Pop-up in selenium using java programming language. Commented Apr 17, 2017 at 4:36
  • Check out this post, stackoverflow.com/questions/10395462/… Commented Aug 25, 2017 at 15:44

4 Answers 4

0

You can check for alert popup. for this you need to import following,

import org.openqa.selenium.security.UserAndPassword;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

First you need to wait until that pop up is coming.

WebDriverWait wait = new WebDriverWait(driver, 30);

Then check for alert popup is present/visible or not

Alert alertPopUp = wait.until(ExpectedConditions.alertIsPresent()); 

Then you can use authenticateUsing method of selenium web driver.

alertPopUp.authenticateUsing(new UserAndPassword("your_username", "your_password"));

There is also anoother way to to quick check, If you want to just verify present of alert

try {
  Alert alert = driver.switchTo().alert();
  alert.accept();

 } catch (NoAlertPresentException e) {
   // Alert not available
   e.printStackTrace();
 }
Sign up to request clarification or add additional context in comments.

3 Comments

Method 'authenticateUsing' does not exist!!
"authenticateUsing" has been removed since Selenium 3. github.com/SeleniumHQ/selenium/issues/6890
I am having no luck using Safari
0

I got this working in IE using the below code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Ie()
driver.get("https://scdm.corp.vha.com")
alert = driver.switch_to_alert()
alert.authenticate("username","password")
time.sleep(20)
driver.close()

Comments

0

Recently I came across this issue while testing the The-Internet Basic authentication Pop-Up. The solution is

driver.get('http://admin:[email protected]/basic_auth')

Add the username and password in the URL itself. I tried it in chrome and firefox both are working.

Comments

0

Copy these 2 files.

name this file manifest.json

{
  "manifest_version": 2,
  "name": "Authentication for ...",
  "version": "1.0.0",
  "permissions": ["<all_urls>", "webRequest", "webRequestBlocking"],
  "background": {
    "scripts": ["background.js"]
  }
}

this one background.js

var username = "your_username";
var password = "your_password";
var retry = 3;

chrome.webRequest.onAuthRequired.addListener(
  function handler(details) {    
    if (--retry < 0)
      return {cancel: true};
    return {authCredentials: {username: username, password: password}};
  },
  {urls: ["<all_urls>"]},
  ['blocking']
);

Zip these files together and put in your project directory, then encode with Base64 and use it as a Selenium extension

private String setUpAuthenticationExtension() {
            byte[] zipBytes;
            try {
                zipBytes = Files.readAllBytes(Path.of("authentication zip file"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return Base64.getEncoder().encodeToString(zipBytes);
        }


ChromeOptions chromeOptions = commonChromeOptions();

chromeOptions.addEncodedExtensions(setUpAuthenticationExtension());


WebDriver driver = new ChromeDriver(chromeOptions);

3 Comments

This is depricated. Do not use this.
It's working for me. Anyway, what you are suggesting to use instead of this?
updated the answer, this one is working for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.