Webbrowser. navigate () in Loop

Source: Internet
Author: User

Recently, when I was writing a small program, I encountered the following requirements:

If you know a set of web page URLs and want to get the HTML of each web page, you actually want to use the webbrowser in the loop statement to load each web page and then get their HTML,

To implement this function, think about it as a very simple task, but in actual operations there is a problem, because the loading of loop statements and webbrowser is not synchronized, leading to the previous

The previous page has not been loaded, and the next cycle starts again .... the final result is that webbrowser only obtains the HTML of the last page. to solve this problem, what we need to do is

Wait until the web page is loaded, and then execute the next loop to load the following web page ....., according to this idea, I wrote the following program, which is effective after testing.

 

Bool loading = true; // The variable indicates whether the webpage is being loaded.
String html = string. empty;
Webbrowser browser = new webbrowser ();

Public void gethtml (string [] URLs)
{
Browser. navigated + = new webbrowsernavigatedeventhandler (browser_navigated );
Foreach (string URL in URLs)
{
Loading = true; // indicates Loading
Browser. navigate (URL );

While (loading)
{
Application. doevents (); // wait until the current loading is completed before the next loop is executed.
}
}
}

Void browser_navigated (Object sender, webbrowsernavigatedeventargs E)
{
Html = browser. documenttext; // The obtained HTML.

Loading = false; // after loading is complete, set this variable to false and execute the next loop immediately.
}

 

 

The above problem is solved, and the following problem occurs: sometimes when a page is loaded, browser_navigated will be executed multiple times.

I checked the online information because the page contains <IFRAME> </iframe>. Each <IFRAME> triggers browser_navigated,

Therefore, the above procedures can be improved as follows:

 

 

Bool loading = true; // The variable indicates whether the webpage is being loaded.
String html = string. empty;
Webbrowser browser = new webbrowser ();

Public void gethtml (string [] URLs)
{
Browser. navigated + = new webbrowsernavigatedeventhandler (browser_navigated );
Foreach (string URL in URLs)
{
Loading = true; // indicates Loading
Browser. navigate (URL );

While (loading)
{
Application. doevents (); // wait until the current loading is completed before the next loop is executed.
}
}
}

Int I = 0;
Void browser_navigated (Object sender, webbrowsernavigatedeventargs E)
{
I ++;
If (I % 3 = 0) // assume that the browser_navigated method is executed three times on each page, this indicates that all the content of the webpage has been loaded. (As for how to get this 3, it's a matter of benevolence, huh, huh)
{
Html = browser. documenttext; // The obtained HTML.

Loading = false; // after loading is complete, set this variable to false and execute the next loop immediately.
}
}

 

 

The above is just a small summary of my work. I have written some notes and hope to help others. I believe there are many ways to solve this problem. I hope you can give me some advice...

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.