經驗總結:WebBrowser自動點擊彈出提示框alert、彈出對話框confirm、屏蔽彈出框、屏蔽彈出腳本錯誤的解決辦法


網上有好多解決方法,可是不一定好使,本人經過多次試驗,針對WebBrowser控件中自動點擊彈出框及禁用腳本提示問題得到如下幾種實際情況的解決辦法,絕對管用。

1、屏蔽彈出錯誤腳本

WebBrowser控件ScriptErrorsSuppressed設置為True即可。 (參考本篇博客:http://www.cnblogs.com/qqflying/archive/2012/07/25/2607881.html

2、頁面一加載就有彈出框的自動點擊(屏蔽)

private  void webBrowser1_Navigated( object sender, WebBrowserNavigatedEventArgs e)
      {
           // 自動點擊彈出確認或彈出提示
          IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
          vDocument.parentWindow.execScript( " function confirm(str){return true;}  "" javascript ");  // 彈出確認
          vDocument.parentWindow.execScript( " function alert(str){return true;}  "" javaScript "); // 彈出提示
      }

3、WebBrowser頁面加載完畢之后,在頁面中進行一些自動化操作的時候彈出框的自動點擊(屏蔽)

private  void webBrowser1_DocumentCompleted( object sender, WebBrowserDocumentCompletedEventArgs e)
        {
             // 自動點擊彈出確認或彈出提示
            IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
            vDocument.parentWindow.execScript( " function confirm(str){return true;}  "" javascript ");  // 彈出確認
            vDocument.parentWindow.execScript( " function alert(str){return true;}  "" javaScript "); // 彈出提示
// 下面是你的執行操作代碼
}

 

注意:一定要放到你的代碼執行前面,這樣有彈出框的時候才會自動點擊,如果把這段代碼放到你點擊按鈕之后,點擊按鈕彈出的提示框是自動點擊不了的。放到前面即可實現點擊按鈕之后自動點擊對話框提示的效果。

另外注意引用 using mshtml;命名控件。


免責聲明!

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



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