Val Nolav
Val Nolav

Reputation: 902

How to make WebBrowser wait till it loads fully?

I have a C# form with a web browser control on it.

I am trying to visit different websites in a loop.

However, I can not control URL address to load into my form web browser element.

This is the function I am using for navigating through URL addresses:

public String WebNavigateBrowser(String urlString, WebBrowser wb)
{
    string data = "";
    wb.Navigate(urlString);
    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
        Application.DoEvents();
    }
    data = wb.DocumentText;
    return data;
}

How can I make my loop wait until it fully loads?

My loop is something like this:

foreach (string urlAddresses in urls)
{
    WebNavigateBrowser(urlAddresses, webBrowser1);
    // I need to add a code to make webbrowser in Form to wait till it loads
}

Upvotes: 5

Views: 30336

Answers (4)

bh_earth0
bh_earth0

Reputation: 2818

what you experiencend happened to me . readyStete.complete doesnt work in some cases. here i used bool in document_completed to check state

 button1_click(){
    //go site1 
    wb.Navigate("site1.com");
    //wait for documentCompleted before  continue to  execute  any further 
    waitWebBrowserToComplete(wb); 

    // set some values in html page
    wb.Document.GetElementById("input1").SetAttribute("Value", "hello");
    //  then click submit. (submit does navigation)
    wb.Document.GetElementById("formid").InvokeMember("submit");
    // then wait for doc complete        
    waitWebBrowserToComplete(wb);


    var processedHtml = wb.Document.GetElementsByTagName("HTML")[0].OuterHtml;
    var rawHtml = wb.DocumentText;
}

// helpers
//instead of checking  readState . we get state from DocumentCompleted Event via bool value
bool webbrowserDocumentCompleted = false;
public static void waitWebBrowserToComplete(WebBrowser wb)
{
  while (!webbrowserDocumentCompleted )
      Application.DoEvents();
  webbrowserDocumentCompleted = false;
}

form_load(){
  wb.DocumentCompleted += (o, e) => {
     webbrowserDocumentCompleted = true;
  };
}

Upvotes: 0

user2887749
user2887749

Reputation: 79

After some time of anger of the crappy IE functionality I've came across making something which is the most accurate way to judge page loaded complete.

Never use the WebBrowserDocumentCompletedEventHandler event use WebBrowserProgressChangedEventHandler with some modifections seen below.

//"ie" is our web browser object

ie.ProgressChanged += new WebBrowserProgressChangedEventHandler(_ie);
private void _ie(object sender, WebBrowserProgressChangedEventArgs e)
{
  int max = (int)Math.Max(e.MaximumProgress, e.CurrentProgress);
  int min = (int)Math.Min(e.MaximumProgress, e.CurrentProgress);
  if (min.Equals(max))
  {
   //Run your code here when page is actually 100% complete
  }
}

Simple genius method of going about this, I found this question googling "How to sleep web browser or put to pause"

Upvotes: 6

Yahia
Yahia

Reputation: 70369

According to MSDN (contains sample source) you can use the DocumentCompleted event for that. Additional very helpful information and source that shows how to differentiate between event invocations can be found here.

Upvotes: 1

SamFisher83
SamFisher83

Reputation: 3995

Add This to your code:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Fill in this function

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
     //This line is so you only do the event once   
     if (e.Url != webBrowser1.Url) 
        return;


        //do you actual code        



    }

Upvotes: 9

Related Questions