How to make WebBrowser wait until it is fully loaded?

I have a C # form with web browser control.

I am trying to visit different sites in a loop.

However, I cannot control the URL to load into the form's web browser element.

This is the function that I use to navigate URLs:

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 looks 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
}
+5
source share
4 answers

Add this to your code:

webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);

Populate 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        



    }
+9
source

After some time of the wrath of the crappy IE function, I came across the creation of something that is the most accurate way to judge a full page load.

WebBrowserDocumentCompletedEventHandler WebBrowserProgressChangedEventHandler , .

// "ie" - -

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
  }
}

, googling " - "

+5

MSDN ( ), DocumentCompleted. , , , .

+1

, , . readyStete.complete . bool document_completed,

 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;
  };
}
0
source

All Articles