0

How can I fill Username and Password fields in alert? When I click a link, another window opens with alert authentication. I'm not able to switch to the alert. I get Timeout Exceptions. I used this code:

// two windows on browser
Set<String> window = driver.getwindowhandles();    
Iterator index = window.ierator();
String Parentwindow = index.next();
String Childwindow = index.next();

driver.switchto().window(Childwindow);

// popup on child window    
Alert alert = driver.switchto().alert();
alert.sendkeys("username");    
alert.sendkeys("password");
barbsan
  • 3,418
  • 11
  • 21
  • 28
Hal
  • 1

1 Answers1

1

This question has been answered before. Selenium has a built in functionality to work with authentication popups.

Using an implicit Wait (this Wait waits until the time runs out for the given condition to become true) you can access the Alert and pass the login-credentials by using authenticateUsing():

WebDriverWait wait = new WebDriverWait(driver, 10);      
Alert alert = wait.until(ExpectedConditions.alertIsPresent());     
alert.authenticateUsing(new UserAndPassword(username, password));

An alternative way would be to provide username and password directly when accessing the URL, which bypasses the Alert entirely:

driver.get("http://UserName:Password@Example.com");

Please refer to this question: How to handle authentication popup with Selenium WebDriver using Java

Tim Hansen
  • 84
  • 2
  • 14
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/22763968) – zmag Apr 16 '19 at 08:35
  • Thanks for your suggestion, too @zmag. I was already typing the other comment. – Tim Hansen Apr 16 '19 at 08:52