Selenium c# accept confirm box

前端 未结 3 673
醉酒成梦
醉酒成梦 2020-12-10 13:14

I have written an nUnit test using selenium in c#.

All was going well until I have to confirm a JS confirm box.

here is the code I am using:

         


        
相关标签:
3条回答
  • 2020-12-10 13:55

    The end point I am testing does not have reliable response times and the only way I could get it to always work with webdriver selenium-dotnet-2.33.0 (.NET4) using Firefox was by doing the following:

    private void acceptAlert(){
    string alertText = "";
    IAlert alert = null;
    while (alertText.Equals("")){
    if (alert == null)
    {
    try{
    alert = driver.SwitchTo().Alert();
    }
    catch{ 
    System.Threading.Thread.Sleep(50); }
    }
    else{
    try{
    alert.Accept();
    alertText = alert.Text;
    }
    catch (Exception ex){
    if (ex.Message.Equals("No alert is present")) alertText = "Already Accepted";
    else System.Threading.Thread.Sleep(50);
    }
    }
    }
    }
    
    0 讨论(0)
  • 2020-12-10 14:00

    in this issue i would try to verify confirm box presence. it be something like:

    this.driver.FindElement(By.Id("submitButton")).Click();
    
    
     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;
    
     }
    

    then if doen't work. try to debug step by step. some additional info concerning alert ( confirm boxes) handle in selenium here hope this somehow helps you

    0 讨论(0)
  • 2020-12-10 14:05

    You just need:

     IAlert alert = driver.SwitchTo().Alert();
     alert.Accept();
    
    0 讨论(0)
提交回复
热议问题