【问题标题】:Handling Alert Windows on Chrome Driver (Webdriver)在 Chrome 驱动程序 (Webdriver) 上处理警报窗口
【发布时间】:2013-09-23 15:54:28
【问题描述】:

我在处理来自 Chrome 的 ALert 弹出窗口时遇到问题,我不断收到以下错误。 org.openqa.selenium.UnhandledAlertException:意外警报打开 (会话信息:chrome=29.0.1547.66) (驱动信息:chromedriver=2.3,platform=Windows NT 5.1 SP3 x86)。

这是我迄今为止尝试过的。当我到达显示错误的页面时:

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

也试过了。

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

同样的错误。

如果有人对此有解决方案,我们将不胜感激。

【问题讨论】:

    标签: java google-chrome selenium webdriver


    【解决方案1】:

    可能是您的 ChromeDriver 版本。我不建议总是更新到最新版本的东西。缺陷比比皆是。

    我正在使用 ChromeDriver win32_2.0,它运行良好。 Try that version.

    【讨论】:

      【解决方案2】:

      其实它(UnhandledAlertException) 如果你没有正确处理警报就会出现,否则,如果你在关闭警报之前对驱动程序实例进行任何操作。

      例子

      第 1 步:单击按钮 //这将导致收到警报

      Step-2: //这里需要alert句柄

      在步骤 2 中,如果您对驱动程序实例执行任何其他操作,而不是处理警报,它将引发 UnhandledAlertException 异常。

      【讨论】:

      • 如果无法使用驱动程序实例,我该如何使用它?因为我不能在不使用驱动程序的情况下创建警报实例。这是我用来处理警报的方法 public static boolean manejarAlertas(){ boolean alerta = true;尝试 { //Espero 10 segundo como maximo a que aparezca el mensaje de alert。 WebDriverWait 等待 = 新的 WebDriverWait(驱动程序, 20); wait.until(ExpectedConditions.alertIsPresent()); driver.switchTo().alert().accept(); //Si esta presente hago click en ok. //alert.accept(); }catch (Exception e){ alerta = false; } 返回警报; }
      • 而不是 WebDriverWait 只是尝试静态等待。我的意思是放一些 Thread.sleep(3000);并尝试相同。如果它有效,则意味着我们可以考虑等待逻辑。
      • 试过了,但问题仍然存在,我什至尝试使用 ActionKey 并按 Key.Esc 并出现同样的错误。
      • 只是好奇,你试过用 FirefoxDriver 吗?工作正常吗?
      【解决方案3】:

      我在 IE 上遇到了这个问题。但是通过两个简单的更改,它开始像在 FF 下一样工作:
      1) 正如https://stackoverflow.com/a/20611297/2872258 建议的那样,我在创建 IEDriver 时设置了另一个选项 - unexpectedAlertBehaviour=Ignore
      2)我还有 WebDriverWait for Alert,一开始就将 ImplicityWait 设置为“0” - 根据@Santoshsarma 所说,这是另一个问题。

      也可能是 Chrome 的解决方案。

      【讨论】:

        【解决方案4】:

        我尝试捕获 stackoverflow 错误,它对我来说是一种解决方法。

        try
        {   
            driver.findElement(By.xpath('xpath')).click(); // command that will trigger the alert window
        }
        catch (StackOverflowError e)
        {
            driver.switchTo().alert().dismiss(); // or driver.switchTo().alert().accept();
            // the rest of the scripts can be added here
        }
        

        【讨论】:

          猜你喜欢
          • 2017-01-31
          • 2015-03-06
          • 1970-01-01
          • 2017-08-11
          • 1970-01-01
          • 2015-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多