Selenium c#接受确认框

4

本文介绍了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 v2.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#接受确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14