显示对话框时,出现异常"UnhandledAlertException:存在模态对话框".被抛出 [英] When I show a dialog, an exception "UnhandledAlertException: Modal dialog present" is thrown

查看:622
本文介绍了显示对话框时,出现异常"UnhandledAlertException:存在模态对话框".被抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Internet Explorer 9使用Selenium Webdriver 2.42.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?

所以有2个选项是/否.

So there are 2 options Yes / No.

我已经使用了driver.switchTo().alert.accept(),但是警报在出现后很快就消失了,不到一秒钟.它不会像应有的那样单击是"按钮.

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.

我的代码:

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; 
        }
    }
}

日志:

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)

推荐答案

警报的消失是因为当抛出UnhandledAlertException时硒会关闭警报.您必须在进行任何其他硒操作之前调用driver.switchTo().alert.accept(),以免抛出抗体.

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();

不确定在您的示例中两次调用driver.switchTo().alert()是否有问题.

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

这篇关于显示对话框时,出现异常"UnhandledAlertException:存在模态对话框".被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆