In previous version of Selenium 2 I have had no choice that to handle alerts in Internet Explorer by overriding the window.alert in Javascript:
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 has support for handling Alerts in IE and Firefox, so you can do the following:
IAlert alert = driver.SwitchTo().Alert();
// Get the text from the alert
string alertText = alert.Text;
// Accept the alert
alert.Accept();
However, I have not been able to get the above to work with Yes/No alerts (Dismiss() works for No but Accept() doesn't work for Yes). I'm in the process of looking at the IEDriver to work out why this is.