C# WinForm call WebBrowser classic strange problem summary

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Recently been researching webpage data acquisition, data acquisition in HtmlAgilityPack alone, is enough.

HtmlAgilityPack interested can have a look here: C# collection agent server IP and set the IE proxy --HtmlAgilityPack real code
But there is a problem: if you want to simulate the manual input data, form submission HtmlAgilityPack do not, Skybound.Gecko (firfox.Net source code)

Can not do this thing. Skybound.Gecko does not support the click, do not know if you have any new method.

Anyway, I can only use Microsoft's own WebBrowser to simulate the webpage click.

At the beginning of all normal, but with the increased functions, there are several problems arise. The following one one lists, and presents the basic solution.

1) Shielding script error dialog

The solution:

webBrowser1.ScriptErrorsSuppressed = true;//I often set to false. Shame ah . 

The 2 shield Js pop-up box (to block the conform)

C# webbrowserPop-up box

Be sure to write to submit events before you!

 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
 vDocument.parentWindow.execScript("function confirm(str){return true;} ", "javascript"); //Confirmation Popup
 vDocument.parentWindow.execScript("function alert(str){return true;} ", "javaScript");//Popup
  element.InvokeMember("click");//Element I need a submit button

Many of the implementation of 3.WebBrowser DocumentCompleted (including a plurality of iframe caused)

In 1, WebBrowser to load a page after the DocumentCompleted event will perform two,
But the two time the ReadyState condition is not the same, respectively is Intercative and Complete.
The MSDN of the two state value is explained:
The Complete control has finished loading a new document and all its contents,
Interactive the control is already loaded enough documentation to allow limited user interaction, such as clicking hyperlinks that have been displayed.
Thus, in fact, the DocumentCompleted event in the Interactive state and the Complete state is called a,
Then we can according to our need to decide what need that time. The solution example code is as follows:

2, if a page contains multiple frames page Iframes, then each frame in the page finishes loading may trigger a DocumentCompleted event
MSDN on the explanation is in multiple frames in case of DocumentComplete acquisition trigger repeatedly.
Not every framework will trigger the event trigger, but each frame DownloadBegin events will trigger the DocumentComplete event.
Solution to the example code in this case are as follows:

4 completely solve the problem: Method 2 pop-up that pop up a dialog box code, solve problems, if continuous pop conform and alert will fail.

Finally, the ultimate method: is the successor to the WebBrowser, blocked pop-up box event, or add your own processing method.

This is a permanent cure. Is not very complicated.

The establishment of a class, inherit from System.Windows.Forms.WebBrowser.

Download the address: http://pan.baidu.com/share/link?shareid=1906864666&uk=1765114824

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:Custom } void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit) { //TODO:Custom } } 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 to be honest unspoken rule too much, not just for him. Bitter force.

If you have help, please click on the recommended oh.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Leo at November 27, 2013 - 9:37 PM