Web browser full load detection

How can I detect when the System.Windows.Forms.WebBrowser control finished loading?

I tried to use the Navigate and DocumentCompleted events, but both of them were raised several times during document loading!

+50
c # winforms webbrowser-control
May 6 '10 at 1:33
source share
9 answers

I think the DocumentCompleted event will fire for all loaded child documents (like JS and CSS). You can look at WebBrowserDocumentCompletedEventArgs in DocumentCompleted and check the Url property and compare it with the URL of the main page.

+30
May 6 '10 at 1:40 a.m.
source share

I have done the following:

 void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath) return; //The page is finished loading } 

The last page loaded is usually for navigation, so this should work.

From here .

+25
May 6 '10 at 5:35
source share

The following should work.

 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //Check if page is fully loaded or not if (this.webBrowser1.ReadyState != WebBrowserReadyState.Complete) return; else //Action to be taken on page loading completion } 
+16
Mar 23 '12 at 7:59
source share

Note that the URL in DocumentCompleted may be different from navigating the URL due to server migration or URL normalization (for example, you go to www.microsoft.com and get http://www.microsoft.com in document completed)

On pages without frames, this event fires once after the download is complete. On pages with multiple frames, this event is triggered for each navigation frame (note support is supported inside the frame, for example, clicking on a link in a frame can move frame by frame to another page). The highest-level navigation frame, which may or may not be a top-level browser, fires the final DocumentComplete event.

In the native code, you compare the sender of the DocumentComplete event to determine if the event is the last event in the navigation or not . However, on Windows Forms, the sender parameter does not end with WebBrowserDocumentCompletedEventArgs. You can break your own event to get the parameter value, or check the readystate property in the browser or frame documents in the DocumentCompleted event handler to see if all frames are in a ready state.

There is a test with the readystate method, as if there is a download manager , and navigation is to the download file, navigation can be canceled by the download manager and readistate will not be completed.

+13
May 6, '10 at 18:59
source share

I had the same issue with several DocumentCompleted events that were released and tried all the above suggestions. Finally, it seems that in my case neither the IsBusy property works, nor the Url property, but the ReadyState seems to me what I need, because it has the status "Interactive" when loading several frames and gets the status "Complete" only after loading the last . That way, I know when a page is fully loaded with all its components.

I hope this helps others too :)

+4
Nov 14 '11 at 13:51
source share

It does not seem to raise DocumentCompleted / Navigated events for external Javascript or CSS files, but it will be used for iframes. As the PC says, compare the WebBrowserDocumentCompletedEventArgs.Url property (I don't have karma to make a comment yet).

+3
May 6 '10 at 2:30 a.m.
source share

If you are using WPF, there is a LoadCompleted event.

If it is Windows.Forms , the DocumentCompleted event must be correct. If there are frames on the page you are loading, your WebBrowser fires a DocumentCompleted event for each frame (see here for more details). I would suggest checking the IsBusy property every time an event is fired, and if it is false, then your page is fully loaded.

+2
May 6 '10 at 2:49
source share

Using the DocumentCompleted event with a page with multiple nested frames did not help me.

I used the Interop.SHDocVW library to control the WebBrowser control as follows:

 public class webControlWrapper { private bool _complete; private WebBrowser _webBrowserControl; public webControlWrapper(WebBrowser webBrowserControl) { _webBrowserControl = webBrowserControl; } public void NavigateAndWaitForComplete(string url) { _complete = false; _webBrowserControl.Navigate(url); var webBrowser = (SHDocVw.WebBrowser) _webBrowserControl.ActiveXInstance; if (webBrowser != null) webBrowser.DocumentComplete += WebControl_DocumentComplete; //Wait until page is complete while (!_complete) { Application.DoEvents(); } } private void WebControl_DocumentComplete(object pDisp, ref object URL) { // Test if it the main frame who called the event. if (pDisp == _webBrowserControl.ActiveXInstance) _complete = true; } 

This code works for me when navigating to a specific URL using the webBrowserControl.Navigate (url) method, but I don’t know how to manage the page when I click the html button using htmlElement.InvokeMember ("click").

0
Aug 29 '13 at 21:17
source share

You can use the ProgressChanged event; the last time it is raised, it will be indicated that the document is fully displayed:

 this.webBrowser.ProgressChanged += new WebBrowserProgressChangedEventHandler(webBrowser_ProgressChanged); 
0
Dec 18 '13 at 0:31
source share



All Articles