WebBrowser DocumentCompleted event fired more than once
DocumentCompleted will fire for each frame in the web page. The hard way is to count off the frames, shows you how to access the DOM:
private int mFrameCount;
private void startNavigate(string url) {
mFrameCount = 0;
webBrowser1.Navigate(url);
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
mFrameCount += 1;
bool done = true;
if (webBrowser1.Document != null) {
HtmlWindow win = webBrowser1.Document.Window;
if (win.Frames.Count > mFrameCount && win.Frames.Count > 0) done = false;
}
if (done) {
Console.WriteLine("Now it is really done");
}
}
The easy way is to check the URL that completed loading:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url.Equals(webBrowser1.Url)) {
Console.WriteLine("Now it is really done");
}
}
Why is WebBrowser_DocumentCompleted() firing twice?
You can check the WebBrowser.ReadyState when the event is fired:
if (browser.ReadyState != WebBrowserReadyState.Complete)
return;
ReadyState will be set to Complete once the whole document is ready.
DocumentCompleted firing multiple times - accepted StackOverflow answer not working
DocumentComplete
may get fired multiple times for many reasons (frames, ajax, etc). At the same time, for a particular document, window.onload
event will be fired only once. So, perhaps, you can do your processing upon window.onload
. I just answered a related question on how that can be done.
How to let the webBrowser_Documentcompleted fire multiple times
The solution is unbelievable. After hours of searching and trying things, I found the answer.
In the properties of webBrowser1, I set the property "AllowNavigation" to false.
If set to false, it only registers the webBrowser1.DocumentCompleted() function only ONCE, but when the AllowNavigation property is set to true(which it is by default), the DocumentCompleted() function repeats.
I have no clue why it works this way and I hope people with the same problem find this answer, as it is the only answer on the net..
Webbrowser control document_completed fires more than once
You will get one DocumentCompleted
event per frame/iframe, that's why there are several. If you don't get the last one it's because some resource is still loading or hanging. It could be an image, a script file, or some iframe.
Your best solution is to add a timeout, and continue your program if you get the last DocumentCompleted
event, or your timeout kicks in.
C# WebBrowser DocumentCompleted event not firing with specific site
As I recall, this can happen in certain scenarios with iframes and AJAX calls.
Have you tried something like this instead:
webBrowser.Navigate("http://www.elinformador.mx");
while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// The page is done loading. Do whatever now.
Related Topics
Wix - How to Run/Install Application Without Ui
Converting a Generic List to a CSV String
Http 404 Page Not Found in Web API Hosted in Iis 7.5
How to Use Showdialog Without Blocking All Forms
Creating a Very Simple Linked List
Singleton by Jon Skeet Clarification
Passing Data to Master Page in ASP.NET MVC
What's the Difference Between Iequatable and Just Overriding Object.Equals()
Why Use Try {} Finally {} with an Empty Try Block
What Is the Point of Lookup<Tkey, Telement>
Check If Value Exists in Datatable
Notify Binding for Static Properties in Static Classes
Implementing Collectionchanged
How to Use Httpclient to Send Content in Body of Get Request