This has been awnsered many times here and at other sites and its working, but I would like ideas to other ways to:
get the ReadyState = Complete after using a navigate or post, without using DoEvents because of all of its cons.
I would also note that using the DocumentComplete event woud not help here as I wont be navigating on only one page, but one after another like this.
wb.navigate("www.microsoft.com")
//dont use DoEvents loop here
wb.Document.Body.SetAttribute(textbox1, "login")
//dont use DoEvents loop here
if (wb.documenttext.contais("text"))
//do something
The way it is today its working by using DoEvents. I would like to know if anyone have a proper way to wait the async call of the browser methods to only then proceed with the rest of the logic. Just for the sake of it.
Thanks in advance.
Here is a "quick & dirty" solution. It's not 100% foolproof but it doesn't block UI thread and it should be satisfactory to prototype WebBrowser control Automation procedures:
Here is a bit more generic version of
testButton_Click
method:[Update]
I have adapted my "quick & dirty" sample by borrowing and sligthly refactoring @Noseratio's
NavigateAsync
method from this topic. New code version would automate/execute asynchronously in UI thread context not only navigation operations but also Javascript/AJAX calls - any "lamdas"/one automation step task implementation methods.All and every code reviews/comments are very welcome. Especially, from
@Noseratio
. Together, we will make this world better ;)Below is a basic WinForms app code, illustrating how to wait for the
DocumentCompleted
event asynchronously, usingasync/await
. It navigates to multiple pages, one after another. Everything is taking place on the main UI thread.Instead of calling
this.webBrowser.Navigate(url)
, it might be simulating a form button click, to trigger a POST-style navigation.The
webBrowser.IsBusy
async loop logic is optional, its purpose is to account (non-deterministically) for the page's dynamic AJAX code which may take place afterwindow.onload
event.If you're looking to do something similar from a console app, here is an example of that.
The solution is simple:
// Move on to your sub-sequence code...
Dirty and quick.. I am a VBA guys, this logic has been working forever, just took me days and found none for C# but I just figured this out myself.
Following is my complete function, the objective is to obtain a segment of info from a webpage: