WebBrowser control - wait for page loading after submit form

10,971

You can try WebBrowser.DocumentCompleted Event Occurs when the WebBrowser control finishes loading a document.

private void Form1_Load(object sender, EventArgs e)
{
   webBrowser1.Navigate("google.com");
   webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
   MessageBox.Show("Completed Now!");
}
Share:
10,971

Related videos on Youtube

Jo Nsc
Author by

Jo Nsc

Updated on June 04, 2022

Comments

  • Jo Nsc
    Jo Nsc almost 2 years

    I am new to c# and its concepts, so i am sorry if this question is kind of dumb. I try to do some automatation using the winforms webbrowser control

    elements = webBrowser1.Document.GetElementsByTagName("input");
    foreach (HtmlElement element in elements)
    {
        if (element.GetAttribute("value") == "Anzeigen")
        element.InvokeMember("click");
    }
    
    while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
    
    // do some math on recived html
    // ......
    
    // show results
    MessageBox.Show(numPlanets.ToString() );
    

    So, to explain it: Im looking for a Button with the value "Anzeigen", simulate a click on it, then wait till NEW page is loaded and do my calculations then. Unfortunately my calculations are done on the OLD HTML content, because its not waiting for the page to load. Strangely if i enter an Thread.Sleep(5000); after the foreach loop, this Sleep is executed BEFORE the click is simulated, and the calculation fails also.

    I just need some synchronous behavior for that click, withouth using an event.

    Hope u can help me with that, sorry for my bad english

    EDIT: Solved it like this: Variable bool webbbrowserfinished = false inside the class, if i want synchronous behavior, i do it like this:

       webbrowserfinished = false;
       // do navigation here
    
       while (!webbrowserfinished)
       {
          Application.DoEvents();
          Thread.Sleep(100);
       }
       webbrowserfinished = false;
    
  • Omar
    Omar over 11 years
    Unfortunately he said "without using an event".
  • Uwe Keim
    Uwe Keim over 11 years
    This seems to use events, too ;-)
  • Jo Nsc
    Jo Nsc over 11 years
    My problem with events is, i have to do this over and over again. I go to a new page, do the math, go to a new page, do the math, go to a new page.... i think this cant be solved elegant with an event, correct me if i am wrong