1. Multiple execution issues with documentcompleted
Some Web pages, will trigger the DocumentCompleted event multiple times, because it is asynchronous, will not block, so if not properly handled, will cause some code is incorrectly executed several times, causing unexpected results.
My general practice is to define a global variable (the member variable of the form in WinForm), as shown in the following code:
Private string_type;Private voidButton1_Click (Objectsender, EventArgs e) {_type="Test"; WebBrowser1.Navigate ("http://abc.com");}Private voidWebbrowser1_documentcompleted (Objectsender, WebBrowserDocumentCompletedEventArgs e) { if(_type = ="Test") {_type="";//The following actions can only be performed once ......... }}
This can also be used in cases where the DocumentCompleted event requires multiple triggers, such as when a Web page is opened, a link is opened, and a Web page opens. At this point, the code is modified to
if (_type = = "Test")
{
......
}
else if (_type = = "Test2")
{
......
}
Or use a switch statement.
Another way to refer to http://www.cnblogs.com/yejq/archive/2012/11/24/2785479.html, that is, to open a different Web page, webbrowser1.documentcompleted + = Multiple processing methods, in the method by detecting different elements to deal with different Web pages. For good or bad.
2. Execute JavaScript
One is to use the InvokeMember method, commonly used is InvokeMember ("click"), to achieve the click button or something. One is using the Invokescript method, which I used to use:
string " var a = ' test '; document.getElementById (' a '). Value = A; " ; WebBrowser1.Document.InvokeScript ("eval"newObject[] {JS} );
Invokescript has an overload that can return a value that can be used to interact with JavaScript in the Web page, which is easy to find, not detailed.
Some experience of the browser Automation 4 WebBrowser control fragmentation issues 2