2

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)
Sufian
  • 6,405
  • 16
  • 66
  • 120
XtremeCore
  • 53
  • 1
  • 7
  • Your question has a mention of a warning. Posting the log might be helpful. – Sufian Aug 08 '14 at 09:54
  • I tried with InternetExplorerDriver 2.42.0 and was able to deal with Modal Dialog. Can you confirm what iedriver version are you with? – Sitam Jana Aug 08 '14 at 13:06
  • its IE Driver V 2.42.0 – XtremeCore Aug 11 '14 at 03:41
  • Could this be the root cause? A bypassed call to fxdriver.modals.clearFlag_ ... cf. https://stackoverflow.com/questions/44568402/how-do-i-manually-mouse-dismiss-a-javascript-alert-and-get-back-the-the-body-o/44592827#44592827 – NevilleDNZ Jun 16 '17 at 23:57

2 Answers2

2

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.

Sebastian
  • 5,721
  • 3
  • 43
  • 69
0

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

Kuladip
  • 154
  • 1
  • 10
  • It would be very helpful if you add little more context in your answer. Like, where to add this code, some link to official documentation which advocates your answer or may be just little more explanation about how it worked in your case. – Bhavik Shah Jun 16 '17 at 08:14
  • For my case, I need to click on Upload button without selecting File and check that an alert message is coming or not. But in my case, while clicking on Upload button the below exception is coming selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: Message: Modal dialog present In my case alert.accept or alert.dismiss is not working So, I did to press Tab and select the Upload button and Press Enter Key through Code. It's working perfectly. – Kuladip Jun 16 '17 at 08:45
  • Thanks for your answer. But, I actually requested you to add this explanation in answer. So that, next time, who ever reads your answer, read entire description. – Bhavik Shah Jun 16 '17 at 08:47
  • Alert alert = driver.switchTo().alert(); alert.dismiss(); OR Robot r = new Robot(); r.keyPress(KeyEvent.VK_ESCAPE); r.keyRelease(KeyEvent.VK_ESCAPE); – Kuladip Jun 16 '17 at 08:48