简体   繁体   中英

Unhandled Alert Exception : Modal Dialog Present (Selenium)

I am facing an Issue wherein I get an Exception saying UnhandledAlertException Modal Dialog Present ,below are some of the traces

org.openqa.selenium.UnhandledAlertException: Modal dialog present: This page is asking you to confirm that you want to leave - data you have entered may not be saved.Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'System info: host: 'NCESEPBLRNRE16', ip: '172.16.137.213', os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_26'Session ID: bda22089-a586-4cd8-b7ac-778c98581e11Driver info: org.openqa.selenium.firefox.FirefoxDriverCapabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=23.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance (NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193) at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:174) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348) at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:389) at org.openqa.selenium.By$ById.findElement(By.java:214) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340) at com.amadeus.selenium.runner.SEPWebDriverWrapper.findElement(SEPWebDriverWrapper.java:141)

I searched for some solutions and got the following snippet to handled the issue

//Now the alert appears. Alert alert = driver.switchTo().alert(); alert.accept();

It works fine , but the alert popup appears in different instances (or different scenarios ) for which the alert cannot be handled in all blocks of code

Please let me know if there is any common approach wherein i can disable this alert popup permanently through some firefox profile settings or anyother generic approaches

I am using Selenium Java with browser as Firefox

Write a method like this:

protected boolean isAlertPresent() {
        try {
          driver.switchTo().alert();
          return true;
        } catch (NoAlertPresentException e) {
          return false;
        }
      }

Call the above method to verify whether alert is present or not as below:

if(isAlertPresent()){
            System.out.println(isAlertPresent());
            driver.switchTo().alert().accept();
        }

This can be solved using combination of Robot class and selenium Alert switch to command.

driver.switchTo().alert();
// invoke Robot class
Robot rb = new Robot();
rb.KeyPress(KeyEvent.VK_ENTER);
rb.KeyPress(KeyRelease.VK_ENTER);

try this out and let me know if any issues. it worked for me because I have faced thi issue earlier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM