问题描述
我在 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#接受确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!