A-A+
去除WebBrowser提示对话框
如果使用WebBrowser做自动化测试或者数据采集,难以避免会遇到脚本错误提示、提示对话框等问题,所以如何去除错误提示,保证程序能够正常运行就成了不可避免的课题。我搜集了一些方法,可以处理不同的情况。
1.去除脚本错误提示:当Javascript等脚本出现错误时,会提示是否继续运行,虽然不会阻塞WebBrowser继续加载,但看起来难免美中不足,可以使用以下方法来消除:
- //设置属性,不再显示脚本错误信息
- myWebBrowser1.ScriptErrorsSuppressed = true;
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");
- //下面是你的执行操作代码
- }
4.重写Alert方法(未经亲自验证,可以尝试):
- private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
- {
- IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
- string s = @"window.alert=null;";
- win.execScript(s, "javascript");
- }
5.实现IDocHostShowUI接口,重新构造自己的WebBrowser,这种方法最干脆,而且可行:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- namespace WindowsFormsApplication7
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- MyWebBrowser MyWebBrowser1 = new MyWebBrowser();
- private void Form1_Load(object sender, EventArgs e)
- {
- this.Controls.Add(MyWebBrowser1);
- MyWebBrowser1.Visible = true;
- MyWebBrowser1.Navigate("about:blank");
- MyWebBrowser1.Document.Write("<script>alert('我就是确定按钮啊')</script>");
- }
- }
- //自定义WebBrowser,并实现IDocHostShowUI接口
- public class MyWebBrowser : WebBrowser
- {
- #region ExtendedWebBrowserSite
- class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
- {
- public ExtendedWebBrowserSite(WebBrowser host)
- : base(host)
- {
- }
- void UnsafeNativeMethods.IDocHostShowUI.ShowMessage(ref UnsafeNativeMethods._RemotableHandle hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, out int plResult)
- {
- plResult = 0;
- //屏蔽符合条件的提示对话框
- if (lpstrText == "我就是确定按钮啊")
- {
- if (MessageBox.Show("是否要屏蔽Alert对话框", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
- {
- MessageBox.Show(lpstrText, lpstrCaption);
- }
- }
- }
- void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
- {
- }
- }
- protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
- {
- return new ExtendedWebBrowserSite(this);
- }
- #endregion
- }
- public class UnsafeNativeMethods
- {
- #region IDocHostShowUI
- [StructLayout(LayoutKind.Explicit, Pack = 4)]
- public struct __MIDL_IWinTypes_0009
- {
- // Fields
- [FieldOffset(0)]
- public int hInproc;
- [FieldOffset(0)]
- public int hRemote;
- }
- [StructLayout(LayoutKind.Sequential, Pack = 4)]
- public struct _RemotableHandle
- {
- public int fContext;
- public __MIDL_IWinTypes_0009 u;
- }
- [StructLayout(LayoutKind.Sequential, Pack = 4)]
- public struct tagPOINT
- {
- public int x;
- public int y;
- }
- [ComImport, Guid("C4D244B0-D43E-11CF-893B-00AA00BDCE1A"), InterfaceType((short)1)]
- public interface IDocHostShowUI
- {
- [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
- void ShowMessage([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrText, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrCaption, [In] uint dwType, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrHelpFile, [In] uint dwHelpContext, [ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.LONG_PTR")] out int plResult);
- [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
- void ShowHelp([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszHelpFile, [In] uint uCommand, [In] uint dwData, [In] tagPOINT ptMouse, [Out, MarshalAs(UnmanagedType.IDispatch)] object pDispatchObjectHit);
- }
- #endregion
- }
- }
可以参考以下资料:
经验总结:WebBrowser自动点击弹出提示框alert、弹出对话框confirm、屏蔽弹出框、屏蔽弹出脚本错误的解决办法