C#Webbrowser中屏蔽彈出窗口及腳本錯誤提示


一、屏蔽腳本錯誤提示

(轉)c# webbrowser 加載網頁出錯解決方法

2009-03-25 19:42

當IE瀏覽器遇到腳本錯誤時瀏覽器,左下角會出現一個黃色圖標,點擊可以查看腳本錯誤的詳細信息,並不會有彈出的錯誤信息框。當我們使用WebBrowser控件時有錯誤信息框彈出,這樣程序顯的很不友好,而且會讓一些自動執行的程序暫停。我看到有人采取的解決方案是做一個窗體殺手程序來關閉彈出的窗體。今天探討的方法是從控件解決問題。 
1、SHDocVw.dll 
在COM時代我們使用的WebBrowser控件是SHDocVw.dll。屏蔽錯誤信息的方法很簡單使用下面的一句就可以搞定。

WebBrowser1.Silent = true;

2、.Net中 
在.Net中提供了托管的WebBrowser可供我們使用,當然我們仍然可以在.Net中使用COM組建SHDocVw.dll,如果使用SHDocVw.dll 
處理錯誤方式和上面的方法一樣。但如果我們是使用.Net組件如何解決這個問題呢? 
這個組件給我們提供了一個方法ScriptErrorsSuppressed 。但是在.net framework2.0中他是不起作用的,據說在低版本中使用如下的方式解決

webBrowser1.ScriptErrorsSuppressed = true;

(據說在.net framework2.0以前是這樣,我沒有使用過) 
那么在.net framework2.0中如何解決這個問題呢? 
有一種方法不能徹底解決,可以部分解決問題這里也介紹給大家。 
//捕獲控件的錯誤 
this.WebBrowser.Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error); 
//對錯誤進行處理 
void Window_Error(object sender, HtmlElementErrorEventArgs e) 

     // 自己的處理代碼 
    e.Handled = true; 
}

3、上面的方法對於多個框架嵌套等等的情形還是不能很好的解決。 
為了徹底解決這個問題,我們借助AxWebBrowser來解決WebBrowser的問題。 
我們定義一個自己的類,他的父類是WebBrowser,以后使用這個類就可以了。在這個類的定義中需要引用SHDocVw。 
class EWebBrowser : System.Windows.Forms.WebBrowser 

    SHDocVw.IWebBrowser2 Iwb2;

    protected override void AttachInterfaces(object nativeActiveXObject) 
    { 
        Iwb2 = (SHDocVw.IWebBrowser2) nativeActiveXObject; 
        Iwb2.Silent = true; 
        base.AttachInterfaces(nativeActiveXObject); 
    }

    protected override void DetachInterfaces() 
    { 
        Iwb2 = null; 
        base.DetachInterfaces(); 
    } 
}

//項目中添加Micrsoft.mshtml引用 
using mshtml;

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) 

    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; 
    vDocument.parentWindow.execScript( 
        "function alert(str){if(str=='zswang')confirm(str);}", "javaScript"); 
}

//frame結構

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) 

    IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; 
    foreach (IHTMLElement vElement in vDocument.all) 
        if (vElement.tagName.ToUpper() == "FRAME") 
        { 
            IHTMLFrameBase2 vFrameBase2 = vElement as IHTMLFrameBase2; 
            vFrameBase2.contentWindow.execScript( 
                "function alert(str){confirm('[' + str + ']');}", "javaScript"); 
        } 

二、屏蔽其它窗口 
            (wb.ActiveXInstance as SHDocVw.WebBrowser).NavigateComplete2 += new DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2);//wb是一個Webbrowser控件 
        //屏蔽一些彈出窗口 
        void wb_NavigateComplete2(object pDisp, ref object URL) 
        { 
            mshtml.IHTMLDocument2 doc = (wb.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2; 
            doc.parentWindow.execScript("window.alert=null", "javascript"); 
            doc.parentWindow.execScript("window.confirm=null", "javascript"); 
            doc.parentWindow.execScript("window.open=null", "javascript"); 
            doc.parentWindow.execScript("window.showModalDialog=null", "javascript"); 
            doc.parentWindow.execScript("window.close=null", "javascript"); 
三、自動確定彈出對話框

Q:winform中如何實現自動點擊webbrowser彈出對話框中的確定按鈕

A:

//using mshtml; 
        //using SHDocVw; 
        private void Form1_Load(object sender, EventArgs e) 
        ...{ 
            this.webBrowser1.Navigate("http://localhost:28512/WebSite2/Default.aspx"); 
            SHDocVw.WebBrowser wb = this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser; 
            wb.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(wb_NavigateComplete2); 
        }

        void wb_NavigateComplete2(object pDisp, ref object URL) 
        ...{ 
            mshtml.IHTMLDocument2 doc = (this.webBrowser1.ActiveXInstance as SHDocVw.WebBrowser).Document as mshtml.IHTMLDocument2; 
            doc.parentWindow.execScript("function alert(str){return ''}", "javascript"); 
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM