【问题标题】:Handling Alert in webdriver 2.21and mozilla11在 webdriver 2.21 和 mozilla11 中处理警报
【发布时间】:2012-06-08 11:20:51
【问题描述】:

我正在使用 Firefox 11 + WebDriver 2.21.0 / WebDriver 2.22.0(都试过了)。

在我的场景中,当我单击一个选项卡时,它会打开一个确认框,然后单击 OK 它会开始从服务器加载新选项卡。

所以我将这种情况处理为:

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

但在点击“mytab”后,它会无限期地等待窗口加载。所以它不会出现在alert.accept() 并且浏览器等待接受确认对话框以加载新页面,所以我最终陷入了死锁状态。

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

请帮忙,这种情况如何处理?

【问题讨论】:

    标签: java webdriver alert mozilla


    【解决方案1】:

    先生,您可能在 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;");
    

    【讨论】:

    • 感谢您的回复 Slanec。我尝试使用 ((JavascriptExecutor)driver).executeScript("arguments[0].click()",element);但它也以同样的方式等待。
    • 当我尝试使用 driver.manage().timeouts().pageLoadTimeout() 时。即使在页面加载后,系统也无法检测到页面加载完成,它会一直等待并在指定时间后超时。
    • 很遗憾听到这个消息。那么我想这是一个错误。
    • 感谢它作为 Mozilla 的一种解决方法。同样在ie8中不起作用。单击“myTab”后,它会抛出 modaldialogException。这样相同的测试用例将无法在两个浏览器上运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多