Some experience of the browser Automation 4 WebBrowser control fragmentation issues 2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.