1

I have a list of sites and some sites show an authentification window when the site is opened, I need to know what sites show this authentication popup, and which are not. I'm trying to verify that authentication browser popup shows after page open. But when I use:

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

I get an error:

no alert open(Session info: chrome=59.0.3071.115)(Driver info: chromedriver=2.30.477691(6ee44a7247c639c0703f291d320bdf05c1531b57),platform=Linux 4.4.0-83-generic x86_64) (WARNING: The server did not provide any stacktrace information)

Of course, an authentication window appears on the site. enter image description here

One more thing, I don't need authorize on this site, I just need to make sure, that the window appeared.

Konstantin
  • 95
  • 1
  • 8
  • Authentication and browser pop up are two different things. – santhosh kumar Aug 07 '17 at 11:01
  • Oh, but can I find this authentication window using selenium? – Konstantin Aug 07 '17 at 11:03
  • It might be simple `div`, but not alert. Try right-click on it. If "Inpect element" option (or similar. depends on your browser) is available, then it's not an alert and could be handled as common web element – Andersson Aug 07 '17 at 11:05
  • @Andersson This is not a `div` . – Konstantin Aug 07 '17 at 11:08
  • Possible duplicate of [How wait for alert box to perform the action in Selenium?](https://stackoverflow.com/questions/23007472/how-wait-for-alert-box-to-perform-the-action-in-selenium) – Andersson Aug 07 '17 at 11:08
  • Possible duplicate of [Python Selenium Alert - Prompt username & password is not working](https://stackoverflow.com/questions/45328654/python-selenium-alert-prompt-username-password-is-not-working) – undetected Selenium Aug 07 '17 at 11:18
  • When the browser is launched, i guess, by default the cursor will be placed in username. If so, we can use sendkeys to enter username, password and enter... – santhosh kumar Aug 07 '17 at 11:37
  • ` I don't need authorize on this site, I just need to make sure, that the window appeared.` What is your exact business case? – undetected Selenium Aug 07 '17 at 11:43
  • I have a list of sites and I need to verify which sites include authentication window and which not. And after that I'll work with sites without authentication window. – Konstantin Aug 07 '17 at 11:53

1 Answers1

-1

You can create/modify the method isAlertPresent as given below and try it. It may help you.

First confirm with below method if the alert present

public boolean isAlertPresent() {
    try{
       WebDriverWait wait = new WebDriverWait(driver, 5);
       wait.until(ExpectedConditions.alertIsPresent());
       return true;
    }
    catch (NoAlertPresentException noAlert) {
      return false;
    }
    catch (TimeoutException timeOutEx){
      return false;
    }
}

If above not work then JavascriptExecutor worked for you. Just take care that you should execute it before clicking the event which invoke alert.

((JavascriptExecutor) driver).executeScript("window.confirm = function(msg) { return true; }");

Note :- do not use it after clicking on event which invoke alert confirmation box. Above code by default set the confirmation box as true means you are accepting/click on ok on all confirmation box on that page if invoked

It's an authentication pop-up. You can handle it like below :-

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

OR

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

To Press ESC :-

Use action class

Actions action = new Actions(driver);
action.sendKeys(Keys.ESCAPE).build().perform();

Robot class code :-

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);

Hope it will help you :)

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • Thanks for the answer . The first solution return **false** and in doesn't work for me, the second solution with **JavascriptExecutor** return **null**. – Konstantin Aug 07 '17 at 11:24
  • 2nd JavascriptExecutor option is set the alert value as true and all alert automatically set as true in other words they will not appears set as they already as ok using JavascriptExecutor. – Shubham Jain Aug 07 '17 at 11:28
  • If first one is showing you false means it's not an alert while it's a application base pop-up instead of browser pop-up.. Can you try directly clicking on ok by extracting XPath or other locator – Shubham Jain Aug 07 '17 at 11:30
  • I don't have any path to this popup/window . You can see such popup on this site for example:[link](https://staging.carfax.si/) – Konstantin Aug 07 '17 at 11:37
  • I have added a code in my answer which show how to handle authentication pop-up – Shubham Jain Aug 07 '17 at 11:42
  • This solution implies that I need to know on which site there is an authentication window, but I do not know this. – Konstantin Aug 07 '17 at 11:54
  • http://learn-automation.com/handle-windows-authentication-using-selenium-webdriver/ – Shubham Jain Aug 07 '17 at 12:01
  • The pop-up you have shared in this url is an authentication pop-up https://staging.carfax.si/ .. – Shubham Jain Aug 07 '17 at 12:02
  • This link show you how to handle browser based pop-up :- https://www.guru99.com/alert-popup-handling-selenium.html – Shubham Jain Aug 07 '17 at 12:02
  • Looks like http://learn-automation.com/handle-windows-authentication-using-selenium-webdriver/ is only way to verify that Auth popup exists , but I don't want to create separate exec file for it. – Konstantin Aug 09 '17 at 12:12
  • what happen in manual way,.. if you press ESC is the pop-up gone? .. if yes then Robot class also works for you – Shubham Jain Aug 09 '17 at 12:15
  • Yes pop-up is gone and after that I see 401 auth error on site... Hmmm.. I can verify that this error exists on site and if yest than site have auth . Looks like a solution! :) – Konstantin Aug 09 '17 at 12:23
  • Cool .. I have added two code in my answer which will help you to press ESC key – Shubham Jain Aug 09 '17 at 12:27
  • what happend you just unaccepted the answer? .. any issues? – Shubham Jain Sep 04 '17 at 12:54
  • oh sorry it was an accident – Konstantin Sep 04 '17 at 13:47