2012-06-08 48 views
1

我使用的是Firefox 11 + WebDriver 2.21.0/WebDriver 2.22.0(两者都尝试过)。在webdriver 2.21和mozilla11中处理警报

在我的情况下,当我点击一个选项卡时,它会打开一个确认框并点击OK它开始从服务器加载新选项卡。

所以我处理这种情况为:

driver.findElement(By.id("myTab")).click(); 
driver.switchTo().alert().accept(); 

但点击“MYTAB”后,它会等待窗口无限期地加载。所以它不是alert.accept(),浏览器等待接受确认对话框来加载新页面,所以我最终处于死锁状态。

此代码在Internet Explorer中运行良好。

请帮忙,如何处理情况?

回答

0

先生,您可能在Selenium WebDriver中发现了一个错误(或至少是不一致)。

看看here以前是否找到过,如果没有这样的bug,请随时联系to file it。与此同时,您可以尝试loading FirefoxDriver with "unstable" loading strategy然后(如果它不够),可能driver.manage().timeouts().pageLoadTimeout()(它只适用于Firefox与“不稳定”设置)。

作为一种变通方法,您可以尝试通过JavaScript点击标签 - 尽管我不知道它是否会或不会工作:

((JavascriptExecutor)driver).executeScript("document.getElementById('myTab').click()"); 


编辑:

作为另一种解决方法(受Selenium RC启发),您可以暂时禁用确认对话框...

// assuming your driver can handle JS ;) 
JavascriptExecutor js = (JavascriptExecutor)driver; 

// stores the original confirm() function and replaces it 
js.executeScript("window.originalConfirm = window.confirm;" 
     + "window.confirm = function(m) { return true; };"); 

driver.findElement(By.id("myTab")).click(); 
// it should not even fire the confirm and just proceed 

// get the confirm back 
js.executeScript("window.confirm = window.originalConfirm;"); 
+0

感谢您的回复Slanec。我尝试使用((JavascriptExecutor)驱动程序).executeScript(“arguments [0] .click()”,element);但它也以相同的方式等待。 – Akarsh

+0

当我尝试使用driver.manage()。timeouts()。pageLoadTimeout()。即使在加载页面后,systen仍无法检测到页面加载完成,它会在指定时间后继续等待并超时。 – Akarsh

+0

我很抱歉听到这个消息。那我猜这是一个错误。 –

相关问题