Is it possible to handle "Authentication Required" Pop-up in selenium which having fields as "UserName" and "Password" using Alert.
4 Answers
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();
}
3 Comments
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
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
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);

http://username:[email protected]/; see also serverfault.com/questions/371907/…