0

I am trying to verify presence of alert windows and then accepting it. While doing that using the below code, when NO alert window was present it is causing an exception to be thrown, which is NOT wanted, thus asked to modify the code.

public void acceptIfAlertPresent()
{
    driver.sleep(2000);
    try{
        if(driver.switchTo().alert() != null){
            String alertMessage=driver.switchTo().alert().getText();
            try
            {   
                driver.switchTo().alert().accept();
            }
            catch (StackOverflowError e)
            {
                 driver.switchTo().alert().accept();                 
            }
            resultMap.putOutput("MessageOnAlertWindow", alertMessage);
        }
    }catch(Exception e){
    }
}

How to modify my method ? Almost all the time, the exception is being thrown from the outer most catch block

PraNuta
  • 629
  • 3
  • 12
  • 33
  • So you want that no exception should be thrown if alert is not present? – Jayesh Doolani Mar 24 '17 at 21:16
  • 1
    Possible duplicate of [How to check if an alert exists using WebDriver?](http://stackoverflow.com/questions/11467471/how-to-check-if-an-alert-exists-using-webdriver) – JeffC Mar 25 '17 at 01:26
  • Yes, the previous answers did not suit well with my requirement that the exception should not be thrown when an alert is NOT present. – PraNuta Mar 25 '17 at 13:23
  • Your basic problem is that Selenium WebDriver assumes that you know what state to expect the page to be in. Attempting to write a method that essentially says, "I don't know if there is going to be an alert show up, but if there is, I want to close it, and ignore everything if no alert shows up," is generally a fool's errand. – JimEvans Mar 25 '17 at 13:42
  • JimEvans: I would assume the case, as per what you laid out. But the alert window which I am talking about dependes up on the data existing in the record at that time, for that User. This can't be predicted for all the User conditions. – PraNuta Mar 29 '17 at 17:44

3 Answers3

0

Add a finally block after your outer catch(Exception e) statement. The finally block is always executed, even if an exception is thrown. In the finally block, you can do nothing and the method will exit safely instead of throwing the exception.

Jayesh Doolani
  • 1,233
  • 10
  • 13
0

If the object to handle the alert, if it is display.. i think simply we can do like below

//chrome
    System.setProperty("webdriver.chrome.driver", "E:\\selenium_setups\\01112017\\chromedriver.exe");
    WebDriver driver=new ChromeDriver();
    driver.get("http://www.seleniumhq.org/");

    try{
        String alertText=driver.switchTo().alert().getText();
        driver.switchTo().alert().accept();
        //any other stuff
    }catch(Exception e){
        System.out.println("exception handled "+e.getMessage());
    }

    System.out.println("after try and catch");
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

Try this , It will not throw any error when NO alert window is present:

try {
    WebDriverWait waitAlert = new WebDriverWait(wb, 30);
    waitAlert.until(ExpectedConditions.alertIsPresent());
    wb.switchTo().alert().accept();
    }
     catch (Exception e) {
        logger.info("Alert Didn't Come");
    }
Anuj Teotia
  • 1,303
  • 1
  • 15
  • 21
  • Dear folks ! I fail to understand how the proposed code is any different from what I was using. If I am not wrong, the exception inside try/catch block is being thrown when the "driver.switchTo().alert()" is executed, becuase when you say "driver.switchTo()" there is no object to switch to and the next method call to it, say by asking "driver.switchTo().alert()", is resulting in exception. Even in your suggestions, this same thing is happening. Please let me know. – PraNuta Mar 27 '17 at 13:41