我正在使用Internet Explorer 9使用Selenium Webdriver 2.42.2登录网站。
当我输入凭据并按下登录按钮时,会弹出一个模态对话框,说明:
您已登录同一物理设备。你希望吗? 从之前的登录&登出再次登录?
因此有2个选项是/否。
我已经使用了driver.switchTo().alert.accept()
但警报在出现后很快就会消失,只需几分之一秒。它没有按原样点击是按钮。
我的代码:
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)
答案 0 :(得分:2)
警报消失是因为selenium在投掷UnhandledAlertException
时会关闭警报。你必须在任何其他selenium操作之前调用driver.switchTo().alert.accept()
以避免抛出异常:
Alert alert = driver.switchTo().alert();
alert.accept();
不确定在示例中调用driver.switchTo().alert()
两次是否有问题。
答案 1 :(得分:0)
alert.dismiss()您可以解除警报。对于我的情况,这个问题解决了这个问题..