处理JS弹出窗口抛出异常的问题:org.openqa.selenium.UnhandledAlertException:模态对话框存在

时间:2014-10-29 10:38:20

标签: java selenium

我是selenium web-driver的新手。我正面临一个问题'处理javascript弹出'我点击一个按钮,点击该按钮后弹出一个ok按钮。当我在selenium ide中记录时,它显示一个(命令) assertAlert和( TARGET )*您的电子报价#514106617764的副本已成功生成并发送到您的电子邮件ID。\ n请继续您的在线购买流程。在表(记录脚本)。所以我写下来代码来处理这个弹出窗口。我正在尝试https://buyonline.aegonreligare.com/cordys/buyonlineesales/startiGuarantee.htm?key=websitedirect#

的igaruntee 的 getQoute页面
try{ 
Actions aa=new Actions(dd);
aa.sendKeys("ENTER");
}


catch(Exception e)
{System.out.println(e)}

try
{
	dd.manage().timeouts().pageLoadTimeout(120, TimeUnit.SECONDS);
	
	wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("check1")));
	WebElement a=dd.findElement(By.id("check1"));
	if(a != null)
	{
		status = 1;	
	    System.out.println("iGurantee_Premium Qoute Page " + status);
	}
	else
	{
		status = 0;
		System.out.println("iGurantee_Premium Qoute Page " + status);
	}
	
	Monitoring_FrameWork.SaveResult(tstartTime,"iGurantee_Premium QoutePage ", status,120);
	
}

catch(Exception e)
{
	status = 0;	
	System.out.println(e);
	System.out.println("iGurantee_Premium Qoute Page " + status);	
    
	
}

但我得到一个例外:

org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: db7cdba6-b905-49e0-9947-947d18761929
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
iGurantee_Premium Qoute Page 0
Oct 29, 2014 3:46:38 AM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id: check1)
org.openqa.selenium.UnhandledAlertException: Modal dialog present
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:45'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_67'
Session ID: db7cdba6-b905-49e0-9947-947d18761929
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=18.0}]
	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:187)
	at org.openqa.selenium.remote.ErrorHandler.createUnhandledAlertException(ErrorHandler.java:168)
	at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:141)
	at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:302)
	at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:331)
	at org.openqa.selenium.By$ById.findElement(By.java:216)
	at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:294)
	at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:523)
	at org.openqa.selenium.support.ui.ExpectedConditions.access$0(ExpectedConditions.java:521)
	at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
	at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:1)
	at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:204)
	at aegonreligare.test.main(test.java:224)

请告诉我如何解决这个问题。 我甚至尝试过使用 警报a = driver.switchTo.alert; a.accept();或a.getText(); 但它引发了一个例外:There is no alert...

1 个答案:

答案 0 :(得分:0)

您需要切换窗口,将焦点设置在弹出窗口上并接受或拒绝。

var alert = Driver.SwitchTo().Alert();
alert.Accept();

用c#编写