6

I test if my WebBrowser is completed with:

webBrowser2.DocumentCompleted += (s, e) =>
{
    // Do stuff  
}

The webpage I am accessing as tons of JS files and iframes and stuff, so I use the below function to make sure it's the actual page that's completed loading.

webBrowser2.DocumentCompleted += (s, e) =>
{
    if (e.Url.AbsolutePath != (s as WebBrowser).Url.AbsolutePath)
    {
        return;
    }       
    // Do stuff    
}   

However, it still doesn't appear to be working. Am I doing something wrong or is this syntactically correct and there's some other error in my code?

sir_thursday
  • 5,270
  • 12
  • 64
  • 118
  • 6
    Lol at the title :D – Lews Therin Aug 19 '13 at 19:47
  • What does the comparison have to do with the handler firing multiple times? I think I'm missing something here. – Lews Therin Aug 19 '13 at 19:49
  • Hahaha I just knew someone would mark duplicate... maybe it is if I'm doing something dumb. – sir_thursday Aug 19 '13 at 19:49
  • Apparently `DocumentCompleted` fires multiple times on certain pages, i.e. when iframes load, when JS files load, etc. I only want to run my code when the page in it's entirety has loaded. Even with the if test I inserted above, the `DocumentCompleted` is still firing multiple times for my pages. – sir_thursday Aug 19 '13 at 19:50
  • http://stackoverflow.com/questions/2328835/why-is-webbrowser-documentcompleted-firing-twice?lq=1 http://stackoverflow.com/questions/2777878/detect-webbrowser-complete-page-loading?lq=1 http://stackoverflow.com/questions/2777878/detect-webbrowser-complete-page-loading – sir_thursday Aug 19 '13 at 19:54
  • Those answers give you a mechanism for checking if the document is completely loaded from within the event handler, they do not specify when the event is fired. The point of eventing is that you do not know when an event defined by the class is going to be fired, but you want to tell the class what to do when it is. In your case, you can't tell the `WebBrowser` class when to fire its event, because you are not the `WebBrowswer` class. What you *can* do is include logic in the event handler that compensates for times when the event is fired and you don't really want to take any action. – Preston Guillot Aug 19 '13 at 19:59
  • Okay- then how **am** I able to specify to the `WebBrowser` class when to fire its event? – sir_thursday Aug 19 '13 at 20:03
  • 1
    What is wrong with `if (browser.ReadyState != WebBrowserReadyState.Complete) return` from the first link? Are you still seeing `DocumentCompleted` events after `browser.ReadyState` became `WebBrowserReadyState.Complete` ? – noseratio Aug 19 '13 at 22:28
  • I found a possible solution. Try with this http://stackoverflow.com/a/18521127/779182 – Frank_FC Aug 30 '13 at 08:31

3 Answers3

8

I use this (from an answer on SO to a similar question):

void BrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
        return; 

    //The page has finished loading.
}
Thundrstorm
  • 181
  • 3
  • 16
  • This works amazingly, but for me, e.Url.AbsolutePath was returning the ads that were on the page so I was comparing against the url that I used in Navigate() so if ((sender as WebBrowser).Url.AbsolutePath != targetPath) – zOqvxf Nov 23 '15 at 17:42
5

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.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
1

Just check the e.Url.AbsolutePath is the actual url that you navigated to.

if (e.Url.AbsolutePath == TheActualURLString) { //This your actual page download complete }

Ammar Bukhari
  • 2,082
  • 2
  • 16
  • 16
  • Not so good ... if the webpage contains scripts then these will be loaded after the main webpage. – IanB Dec 10 '14 at 18:48