Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowser control - wait for page loading after submit form

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;
like image 280
Jo Nsc Avatar asked Apr 16 '26 08:04

Jo Nsc


1 Answers

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!");
}
like image 157
Arjun Shetty Avatar answered Apr 17 '26 21:04

Arjun Shetty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!