gpt4 book ai didi

java - 当我显示对话框时,抛出异常 "UnhandledAlertException: Modal dialog present"

转载 作者:行者123 更新时间:2023-11-30 09:04:28 25 4
gpt4 key购买 nike

我正在使用 Internet Explorer 9 登录一个使用 Selenium Webdriver 2.42.2 的网站。

当我输入凭据并按下登录按钮时,会弹出一个模态对话框:

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

所以有 2 个选项是/否。

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

我的代码:

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)

最佳答案

警报消失是因为 selenium 在抛出 UnhandledAlertException 时关闭了警报。您必须在任何其他 selenium 操作之前调用 driver.switchTo().alert.accept() 以避免抛出异常:

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

不确定调用 driver.switchTo().alert() 两次是否有问题。

关于java - 当我显示对话框时,抛出异常 "UnhandledAlertException: Modal dialog present",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com