如何处理Selenium中的IE弹出窗口
带有文本的Internet Explorer弹出窗口您确定要离开此页面吗?是WindowEventHandlers.onbeforeunload
的结果
onbeforeunload mixin的WindowEventHandlers属性是用于处理EventHandler事件的beforeunload。当一个窗口即将unload其资源时,这些事件将触发。此时,文档仍然可见,事件仍可取消。
有不同的策略可用于处理此弹出窗口。但是,作为跨浏览器解决方案,您可以禁用此对话框,调用executeScript()
将window.onbeforeunload
设置为function() {};
,您可以使用以下解决方案:
((JavascriptExecutor)driver).executeScript("window.onbeforeunload = function() {};");
您可以尝试通过selenium接受警报。不确定您使用的是哪种语言,但以下Java方法应该接受警报并让您继续生活。
public void checkAlert()
{
try
{
// Wait for the alert to show
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
}
catch (Exception e)
{
//exception handling
}
}
您需要将import org.openqa.selenium.Alert;
添加到导入中(如果您使用的是Java,则再次)