在以前版本的硒2我别无选择,要处理的警报Internet Explorer中通过重写JavaScript中的window.alert:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
// Override window.alert to store the prompt and accept it automatically
js.ExecuteScript("window.alert = function(msg) { document.bAlert = true; document.lastAlert=msg; }");
// Do some stuff...
// Check for alert
Object o = js.ExecuteScript("return document.bAlert");
if (o != null && (bool)o == true)
{
//retrieve the alert message
o = js.ExecuteScript("return document.lastAlert");
// Do something with the alert text
}
硒2.0b3有用于处理在IE和Firefox警报的支持,这样你就可以做到以下几点:
IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();
但是,我还没有能够得到上面的是/否警报(Dismiss()适用于No,但Accept()不适用于Yes)。我正在查看IEDriver以确定这是为什么。
我重新格式化了你的代码。是否可以翻译中文标志?也许他们透露一些信息。 – GvS 2011-03-02 09:15:44
嘿,查看我的回答 – Johnydep 2012-09-03 02:55:16