Selenium: UnhandledAlertException was unhandled

╄→尐↘猪︶ㄣ 提交于 2019-12-13 04:32:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!