1

I need to open a site that contain authentication alert. To handle that alert I tried following code but no success:

try {
    WebDriverWait wait = new WebDriverWait(driver, 100);
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
     alert.authenticateUsing((Credentials) new UsernamePasswordCredentials("username", "pass"));
} catch (Exception e) {
    System.out.println("Alert not found");
}

Exception occur is:

Expected condition failed: waiting for alert to be present (tried for 100 second(s) with 500 MILLISECONDS interval)

enter image description here

3
  • Are you sure it's an alert and not a modal?
    – FDM
    May 27, 2017 at 8:04
  • Let me share a screenshot
    – Rabia Asif
    May 27, 2017 at 10:33
  • @FDM added a screenshot to a question please see
    – Rabia Asif
    May 27, 2017 at 10:37

1 Answer 1

2

If I'm not mistaken, that's a basic authenticatin popup.

The easy way to bypass this in Selenium tests is to insert the username and password in the URL, like so:

@driver.get 'http://admin:admin@the-internet.herokuapp.com/basic_auth'

More info: http://elementalselenium.com/tips/13-work-with-basic-auth Let me know if this works for you!

Alternative 1 Using AutoIt, or Windows API which is same as alternative 3 but proper code. See the answers in this StackOverflow topic.

Alternative 2 See Mike's answer in this StackOverflow topic on how to create a Chrome extension to bypass the popup

Alternavite 3 (works but quick & dirty)

using System.Windows.Forms
System.Threading.Thread.Sleep(2000);
SendKeys.SendWait("username");
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("password");
SendKeys.SendWait("{ENTER}");
4
  • Okay I just tried your solution, site successfully opened but again the popup raised
    – Rabia Asif
    May 27, 2017 at 11:16
  • Do you have an Url where I can test this popup?
    – FDM
    May 27, 2017 at 20:05
  • Yes Please: engprod-charter.net
    – Rabia Asif
    May 27, 2017 at 21:26
  • See my edits for some alternatives which work (I have personally tried alternative 3)
    – FDM
    May 28, 2017 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.