【问题标题】:Special case of handling alerts in eclipse/java seleniumeclipse/java selenium中处理警报的特例
【发布时间】:2017-07-03 09:47:26
【问题描述】:

我想处理警报并不总是显示 例如: 我使用凭据登录到系统:eee / eee_123 如果在我之前已经有其他用户登录,则会显示警报告诉我是否想将他踢出,但如果我是唯一一个使用这些凭据的用户,我想在没有警报的情况下成功登录。

我的问题:如何处理这个警报我使用了这个条件: if (ExpectedConditions.alertIsPresent() != null) driver.switchTo().alert().accept();

它不工作

【问题讨论】:

  • 你说的不工作是什么意思。会发生什么?
  • 我的意思是只有当警报显示时条件才有效,但是当没有警报显示时测试用例失败并停止运行硒

标签: javascript java eclipse selenium-webdriver alert


【解决方案1】:

可能与 try catch 一起使用

Boolean bflag = false;

try {
        WebDriverWait wait = new WebDriverWait(driver, 2);
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert().accept();
     } catch (NoAlertPresentException e) {
        bflag = true;
    }

if (blag ==true) {//alert is not there}

【讨论】:

    【解决方案2】:

    ExpectedConditions.alertIsPresent() 不返回 null。如果找到则返回alert,否则抛出TimeoutException。尝试切换到try catch块内的alert

    try 
    { 
        driver.switchTo().alert().accept();
    }   
    catch (NoAlertPresentException Ex) { }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-02
      • 2017-06-29
      • 2012-01-04
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多