Selenium WebDriver with Java: Can't accept alert

前端 未结 3 1565
忘掉有多难
忘掉有多难 2020-12-18 14:58

When recording in selenium IDE I can click the \"OK\" button in a popup, and expected to be able to click it using

driver.findElement(By.linkText(\"OK\")).cl         


        
3条回答
  •  醉梦人生
    2020-12-18 15:13

    in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it. It be somthing like:

    public boolean isAlertPresent() {
    
      boolean presentFlag = false;
    
      try {
    
       // Check the presence of alert
       Alert alert = driver.switchTo().alert();
       // Alert present; set the flag
       presentFlag = true;
       // if present consume the alert
       alert.accept();
    
      } catch (NoAlertPresentException ex) {
       // Alert not present
       ex.printStackTrace();
      }
    
      return presentFlag;
    
     }
    

    here you can get details Also do not forget about debug step by step.

    Hope this helps you.

提交回复
热议问题