简体   繁体   English

硒C#接受确认框

[英]Selenium c# accept confirm box

I have written an nUnit test using selenium in c#. 我已经在C#中使用硒编写了一个nUnit测试。

All was going well until I have to confirm a JS confirm box. 一切顺利,直到我必须确认JS确认框。

here is the code I am using: 这是我正在使用的代码:

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

The confirm box appears after the submit button. 确认框出现在提交按钮之后。 The confirm appears and then disappears immediately but the form does not submit. 确认出现,然后立即消失,但不提交表单。 The behaviour is the same regardless of the accept() line above. 无论上面的accept()行如何,其行为都是相同的。

I am using Firefox v15.0.1 and selenium v2.24 我正在使用Firefox v15.0.1和硒v2.24

I have tried putting a Thread.Sleep between the submit click and the confirm accept. 我试图在提交点击和确认接受之间放置一个Thread.Sleep。

Everything I have read has said that the selenium driver will automatically send a confirm OK, but something else seems to be happening. 我读过的所有内容都表明,硒驱动程序将自动发送确认OK,但是似乎正在发生其他事情。

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 关于警报(确认框)一些额外的信息处理硒这里希望这在某种程度上可以帮助你

You just need: 您只需:

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

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: 我要测试的终点没有可靠的响应时间,并且我可以使它始终与使用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);
}
}
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM