-1

Is there any way to recognise whether the below authentication popup has occurred or not ?

enter image description here

I want to insert the username and password only when the authentication popup occurs else not.

bool popupOccurred = false;

//How to recognize whether popup has occurred or not here    

 if (popupOccurred)
  {
     AutoItX.Send(username);
     Thread.Sleep(1000);
     AutoItX.Send("{TAB}");
     Thread.Sleep(1000);
     AutoItX.Send(password);
     Thread.Sleep(1000);
     AutoItX.Send("{ENTER}");
   }
user4157124
  • 2,809
  • 13
  • 27
  • 42
DotNetSpartan
  • 931
  • 4
  • 20
  • 41

2 Answers2

0

You can use explicit waits and wait till a specific element is available if not, it will continue with the next line of execution.

WebElement loginPopup = (new WebDriverWait(driver, 20)).until(ExpectedConditions.presenceOfElementLocated(By.id("ELEMENT_ID")));
nu1silva
  • 149
  • 1
  • 9
  • The Popup seen in my post is not a browser window, But OS’s authentication popup, Similar to the popup here (https://www.engprod-charter.net/api/pub/cnetloginedge/spectrum-redir?targetUrl=https%3A%2F%2Fwww.engprod-spectrum.net%2F%3Fdomain-redirect%3Dtrue). Hence selenium doesn’t have control over it, Can you please provide more clues how can i get the element id ? – DotNetSpartan Apr 02 '19 at 07:39
  • Ah, thanks for the clarification. I'm not sure if you have encountered this blog post https://www.joecolantonio.com/selenium-how-to-handle-windows-based-dialogs-and-pop-ups/ Seems to be covering the issue you have. I did not try this yet, but thought of sharing. – nu1silva Apr 02 '19 at 08:22
0

You don't need to use AutoITX for what you want to do.

The AutoITX library you use, we use it only when we want to "select" a file to upload (when the a popup appears that will show the files in your system).

What you see here is an "Alert" and there are a lot of implementations in selenium itself that lets you handle the alert.

Think of an alert as a seperate "browser tab" that you want go to it (switch to it), and handle elements on it. There are a lot of different types of alerts. Some will simply appear and have an OK button, some will have a Yes or No, and others (like yours), will be an authentication form.

You can tell your selenium webdriver to switch to alert (when it is shown), then enter the credentials and click OK. All through the webdriver, with no AutoITX needed.

A simple implementation will be:

//Switch to the alert
IAlert alertHandler = driver().SwitchTo().Alert();
//Send the credentials
alertHandler.SetAuthenticationCredentials("Username", "Password");
//Switch back to the default content (your main tab).
driver().SwitchTo().DefaultContent();

There are also implementations for other kinds of alerts

//Accepts the "OK" alert or clicks "Yes" on the yes/no alert.
alertHandler.Accept();
//Clicks Close/Dismiss/No
alertHandler.Dismiss();

And to answer your original question as to HOW to WAIT for an alert, here is my implementation

public static bool IsAlertPresent()
{
    int secondsToWait = 1; //Wait for 1 second.
    try
    {
        new WebDriverWait(driver, TimeSpan.FromSeconds(secondsToWait)).Until(ExpectedConditions.AlertIsPresent());
        return true;
    }
    catch (WebDriverTimeoutException) { return false; }
}
  • 1
    The IsAlertPresent() does not returns me true and is failing with an exception : "The HTTP request to the remote WebDriver server for URL http://localhost:9515/session/0e53c17d68e7e898dc0ab98cddde7b1f/alert_text timed out after 30 seconds." – DotNetSpartan Apr 02 '19 at 09:28
  • @MishraSaurabh Same here... totally great and unobstrusive solution, but it doesn't seem to work with `ChromeDriver`. – timmkrause Jan 28 '21 at 15:44
  • @MishraSaurabh Don't know about this really (I was able to achieve this using chromedriver), but, you can do this is a more "dirty" way, by using 'driver().SwitchTo().Alert()' in a try-catch endless loop, and only exit if it has successfully switched to the alert. Tell me if you need a code example and I will edit the answer. – Thodoris Koskinopoulos Jan 29 '21 at 15:01