Selenium c#接受确认框

Selenium c# accept confirm box(Selenium c#接受确认框)
本文介绍了Selenium c#接受确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

一切顺利,直到我必须确认一个 JS 确认框.

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

这是我正在使用的代码:

here is the code I am using:

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

确认框出现在提交按钮之后.确认出现然后立即消失,但表单未提交.无论上面的 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.

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

I am using Firefox v15.0.1 and selenium v2.24

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

I have tried putting a Thread.Sleep between the submit click and the confirm accept.

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

Everything I have read has said that the selenium driver will automatically send a confirm OK, but something else seems to be happening.

推荐答案

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

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;

 }

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

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

这篇关于Selenium c#接受确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)