XtremeCore
XtremeCore

Reputation: 53

When I show a dialog, an exception "UnhandledAlertException: Modal dialog present" is thrown

I am using internet explorer 9 to login a website using Selenium Webdriver 2.42.2.

When I enter the credentials and press the login button, a modal dialog box pops up saying :

You are already logged on to the same physical device. Do you wish logout from the previous login & login again?

So there are 2 options Yes / No.

I have used the driver.switchTo().alert.accept() but the alert just disappears quickly after appearing, in fraction of a second. It does not click on the Yes button, as it should.

My code:

public class launch{
    public static void main(Sting args[]){
        driver.findElement(By.id("login")).click();// click performed

        launch obj = new launch();
        if(obj.isAlertPresent(driver) == true)
        {
            Thread.sleep(3000);
            driver.switchTo().alert();
            String a = driver.getTitle();
            System.out.println(a);
        }
    }
    public boolean isAlertPresent(WebDriver drive) throws InterruptedException 
    { 
        try 
        { 
            Thread.sleep(2000);
            drive.switchTo().alert(); 
            Thread.sleep(2000);
            return true; 
        }   
        catch (UnhandledAlertException Ex) 
        { 
            return false; 
        }
    }
}

Log:

Started InternetExplorerDriver server (64-bit)
2.42.0.0
Listening on port 5600
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: Modal dialog present: You are already logged on to the same physical device. Do you wish logout from the previous login & login again ?

Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=9, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, , takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: e439a04d-98aa-45e3-ae87-ec30e6f2cd2a
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:185)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:152)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:614)
    at org.openqa.selenium.remote.RemoteWebDriver.getWindowHandle(RemoteWebDriver.java:489)
    at OneWebTest.webrio_launch.main(webrio_launch.java:29)

Upvotes: 2

Views: 8967

Answers (2)

Kuladip
Kuladip

Reputation: 154

alert.dismiss() You can dismiss the alert. For my case for this is solved this issue..

Upvotes: 0

Sebastian
Sebastian

Reputation: 6057

The disappearing of the alert is because selenium closes the alert when throwing an UnhandledAlertException. You have to call driver.switchTo().alert.accept() before any other selenium operation in order to avoid the exeption to be thrown:

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

Not sure if it is a problem to call driver.switchTo().alert() twice as you do in your example.

Upvotes: 2

Related Questions