DocumentCompleted firing multiple times - accepted StackOverflow answer not working

耗尽温柔 提交于 2019-12-30 06:36:13

问题


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?


回答1:


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.




回答2:


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.
}



回答3:


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 }



来源:https://stackoverflow.com/questions/18321872/documentcompleted-firing-multiple-times-accepted-stackoverflow-answer-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!