问题
I have been running automation testing on selenium (C#) for quite some time now, and I recently upgraded to version 2.37 with Chrome Driver 2.4. After this upgrade, for some reason every time an open dialog causes a test to fail, the exception is unhandled, which causes Visual Studio to completely freeze up and the rest of the test suite does not get executed.
Is there a good way to prevent this from occurring other than trying to avoid an alert from causing a test to fail? It is nice to test for that, so I do not want to simply dodge the issue. After a failing test, I attempt to switch to DefaultContent, and that is specifically where the "UnhandledAlertException was unhandled" gets thrown:
Driver.SwitchTo().DefaultContent();
It is in a try-catch block, but this exception does not get caught for some strange reason.
回答1:
Driver.SwitchTo().DefaultContent();
is for switching frames. Quote from API:
Selects either the first frame on the page or the main document when a page contains iFrames.
You might want to try IAlert
in WebDriver.dll.
For example
try {
// doing your thing
} catch (UnhandledAlertException) {
IAlert alert = driver.SwitchTo().Alert();
Console.WriteLine(alert.Text);
alert.Accept(); // or alert.Dismiss(); depends on your needs
}
来源:https://stackoverflow.com/questions/19550514/selenium-unhandledalertexception-was-unhandled