【问题标题】:Selenium c# accept confirm boxSelenium c#接受确认框
【发布时间】:2012-10-05 10:45:18
【问题描述】:

我在 c# 中使用 selenium 编写了一个 nUnit 测试。

在我必须确认一个 JS 确认框之前一切都很顺利。

这是我正在使用的代码:

this.driver.FindElement(By.Id("submitButton")).Click();
this.driver.SwitchTo().Alert().Accept();

确认框出现在提交按钮之后。确认出现然后立即消失,但表单没有提交。无论上面的 accept() 行如何,行为都是相同的。

我正在使用 Firefox v15.0.1 和 selenium v​​2.24

我尝试在提交点击和确认接受之间放置一个 Thread.Sleep。

我读过的所有内容都说硒驱动程序会自动发送确认 OK,但似乎正在发生其他事情。

【问题讨论】:

  • 是我自己的错,没有bug。我在测试中查看了错误的提交按钮:(。完成程序员错误。下面接受了解决方案,因为它是选择警报框的正确方法。

标签: c# selenium nunit webdriver selenium-webdriver


【解决方案1】:

在这个问题中,我将尝试验证确认框的存在。 它是这样的:

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;

 }

如果不工作。尝试逐步调试。 selenium here 中有关警报(确认框)句柄的一些附加信息 希望这对您有所帮助

【讨论】:

    【解决方案2】:

    你只需要:

     IAlert alert = driver.SwitchTo().Alert();
     alert.Accept();
    

    【讨论】:

      【解决方案3】:

      我正在测试的端点没有可靠的响应时间,我可以让它始终使用 Firefox 的 webdriver selenium-dotnet-2.33.0 (.NET4) 工作的唯一方法是执行以下操作:

      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);
      }
      }
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-02-22
        • 1970-01-01
        • 1970-01-01
        • 2015-12-13
        • 2011-08-01
        • 1970-01-01
        • 2015-11-28
        • 1970-01-01
        相关资源
        最近更新 更多