How to check if an alert exists using WebDriver?

后端 未结 9 1770
名媛妹妹
名媛妹妹 2020-12-01 09:09

I need to check the existence of Alert in WebDriver.

Sometimes it pops up an alert but sometimes it will not pop up. I need to check if the alert exists first, then

9条回答
  •  忘掉有多难
    2020-12-01 09:27

    public static void handleAlert(){
        if(isAlertPresent()){
            Alert alert = driver.switchTo().alert();
            System.out.println(alert.getText());
            alert.accept();
        }
    }
    public static boolean isAlertPresent(){
          try{
              driver.switchTo().alert();
              return true;
          }catch(NoAlertPresentException ex){
              return false;
          }
    }
    

提交回复
热议问题