以前のバージョンのSelenium2では、Javascriptでwindow.alertをオーバーライドすることにより、InternetExplorerでアラートを処理する選択肢がありませんでした。
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
}
Selenium 2.0b3は、IEおよびFirefoxでのアラートの処理をサポートしているため、次のことができます。
IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();
ただし、上記をYes / Noアラートで機能させることができませんでした(Dismiss()はNoで機能しますが、Accept()はYesでは機能しません)。私はこれがなぜであるかを解明するためにIEDriverを調べているところです。