WebBrowser屏蔽alert是用重定義alert,confirm實(shí)現(xiàn)的。比較簡(jiǎn)單。代碼如下:
添加 com 引用 microsoft html object library
using mshtml;
1、頁面一加載就有彈出框的自動(dòng)點(diǎn)擊(屏蔽)
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
IHTMLWindow2 win = (IHTMLWindow2)webBrowser1.Document.Window.DomWindow;
string s = @"function confirm() {return true;}";
s += @"function alert() {}";
win.execscript(s, "javascript");
}
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
//自動(dòng)點(diǎn)擊彈出確認(rèn)或彈出提示
IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
vDocument.parentWindow.execscript("function confirm(str){return true;} ", "javascript"); //彈出確認(rèn)
vDocument.parentWindow.execscript("function alert(str){return true;} ", "javascript");//彈出提示
}
2、WebBrowser頁面加載完畢之后,在頁面中進(jìn)行一些自動(dòng)化操作的時(shí)候彈出框的自動(dòng)點(diǎn)擊(屏蔽)
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//自動(dòng)點(diǎn)擊彈出確認(rèn)或彈出提示
IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
vDocument.parentWindow.execscript("function confirm(str){return true;} ", "javascript"); //彈出確認(rèn)
vDocument.parentWindow.execscript("function alert(str){return true;} ", "javascript");//彈出提示
//下面是你的執(zhí)行操作代碼
}
我的不是這樣弄的,雖然代碼一樣,但是我是在執(zhí)行具體任務(wù)的時(shí)候,才會(huì)寫入這個(gè),然后執(zhí)行這段代碼+我自己的點(diǎn)擊代碼。
剛才在網(wǎng)上找了下,還有另一種治本的方法呵,高人到處都是。下面是代碼,是繼承的webBrowser然后屏弊了ShowMessage
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Test
{
public class MyWebBrowser : System.Windows.Forms.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;
//TODO:自定義
}
void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
{
//TODO:自定義
}
}
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
}
}
這個(gè)是直接復(fù)制過來的啊,這編輯器沒有代碼功能,汗。。。有點(diǎn)亂。但很好的屏弊webBrowser里的alert,confirm了。
不管怎么樣,有二種方法,可以實(shí)現(xiàn)webBrowser屏弊alert了。
該文章在 2022/12/15 15:51:02 編輯過