取消“您确定要离开此页面吗".NET 浏览器控件中的弹出窗口 [英] Supress the "are you sure you want to leave this page" popup in the .NET webbrowser control

查看:37
本文介绍了取消“您确定要离开此页面吗".NET 浏览器控件中的弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 WinForms C# 编写的 Web 浏览器自动化项目.在导航过程中,浏览器会执行您确定要离开此页面吗?"的位置.弹出.我们需要这个弹出窗口,所以我无法从网站代码中删除它,这意味着我必须在我的自动化应用中覆盖它.

I have a web browser automation project written in WinForms C#. During the navigation there is a point where the browser does the "are you sure you want to leave this page?" popup. We need this popup, so I cannot remove it from the website code, which means I have to override it in my automation app.

有人知道如何做到这一点吗?

Does anyone have an idea how to do this?

推荐答案

这是一个顺利的解决方案..

and here was the smooth solution..

添加对mshtml的引用并使用mshtml添加;

add a reference to mshtml and add using mshtml;

Browser.Navigated += 
    new WebBrowserNavigatedEventHandler( 
        (object sender, WebBrowserNavigatedEventArgs args) => { 
            Action<HtmlDocument> blockAlerts = (HtmlDocument d) => { 
                HtmlElement h = d.GetElementsByTagName("head")[0]; 
                HtmlElement s = d.CreateElement("script"); 
                IHTMLScriptElement e = (IHTMLScriptElement)s.DomElement; 
                e.text = "window.alert=function(){};"; 
                h.AppendChild(s); 
            }; 
            WebBrowser b = sender as WebBrowser; 
            blockAlerts(b.Document); 
            for (int i = 0; i < b.Document.Window.Frames.Count; i++) 
                try { blockAlerts(b.Document.Window.Frames[i].Document); } 
                catch (Exception) { }; 
        } 
    ); 

这篇关于取消“您确定要离开此页面吗".NET 浏览器控件中的弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆