Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DocumentCompleted firing multiple times - accepted StackOverflow answer not working

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?

like image 529
sir_thursday Avatar asked Aug 19 '13 19:08

sir_thursday


2 Answers

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.
}
like image 143
Thundrstorm Avatar answered Sep 20 '22 18:09

Thundrstorm


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.

like image 25
noseratio Avatar answered Sep 21 '22 18:09

noseratio