Selenium C# 接受确认框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12744576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:17:46  来源:igfitidea点击:

Selenium c# accept confirm box

c#seleniumnunitwebdriverselenium-webdriver

提问by Tim Jarvis

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

我在 c# 中使用 selenium 编写了一个 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 和 selenium v​​2.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.

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

采纳答案by eugene.polschikov

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 herehope this somehow helps you

那么如果不起作用。尝试一步一步调试。关于警报(确认框)一些额外的信息处理硒这里希望这在某种程度上可以帮助你

回答by Christopher Akritidis

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

回答by Danny

You just need:

您只需要:

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