检测WebBrowser完整页面加载

时间:2010-05-06 01:33:02

标签: c# winforms webbrowser-control

如何检测System.Windows.Forms.WebBrowser控件何时完成加载?

我尝试使用Navigate和DocumentCompleted事件,但在文档加载期间它们都被引发了几次!

9 个答案:

答案 0 :(得分:32)

我认为DocumentCompleted事件将被加载所有加载的子文档(例如JS和CSS)。您可以查看WebBrowserDocumentCompletedEventArgs中的DocumentCompleted并检查Url属性,并将其与主页的Url进行比较。

答案 1 :(得分:28)

我做了以下事情:

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

  //The page is finished loading 
}

加载的最后一页往往是导航到的页面,所以这应该有效。

来自here

答案 2 :(得分:16)

以下情况应该有效。

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
}

答案 3 :(得分:13)

请注意,由于服务器转移或网址规范化,DocumentCompleted中的网址可能与导航网址不同(例如,您导航到www.microsoft.com并在文档完成中获得http://www.microsoft.com

在没有框架的页面中,此事件在加载完成后会触发一次。在具有多个框架的页面中,此事件针对每个导航框架触发(框架内支持注释导航,例如,单击框架中的链接可将框架导航到另一个页面)。最高级别的导航框架(可能是也可能不是顶级浏览器)会触发最终的DocumentComplete事件。

在原生代码中,您会compare the sender of the DocumentComplete event to determine if the event is the final event in the navigation or not。但是,在Windows窗体中,发送者参数不包含在WebBrowserDocumentCompletedEventArgs中。您可以sink the native event获取参数的值,或者检查DocumentCompleted事件处理程序中的readystate property of the browserframe documents,以查看是否所有帧都处于就绪状态。

使用readystate方法存在一个问题,就好像存在download manager并导航到可下载文件一样,导航可以被下载管理器取消,而状态状态也不会完整。

答案 4 :(得分:4)

我遇到了多个DocumentCompleted被解雇事件的问题,并尝试了上述所有建议。最后,似乎在我的情况下IsBusy属性既不正常也不Url属性,但ReadyState似乎是我需要的,因为它在加载多个时具有'Interactive'状态帧,只有在加载最后一个帧后才会获得状态“完成”。因此,我知道页面何时完全加载了所有组件。

我希望这也有助于其他人:)

答案 5 :(得分:3)

它似乎没有触发外部Javascript或CSS文件的DocumentCompleted / Navigated事件,但它将用于iframe。正如PK所说,比较WebBrowserDocumentCompletedEventArgs.Url属性(我还没有业力来发表评论)。

答案 6 :(得分:3)

如果您使用WPF,则会发生LoadCompleted事件。

如果是Windows.Forms,则DocumentCompleted事件应该是正确的。如果您加载的网页包含框架,则WebBrowser控件会针对每个框架触发DocumentCompleted事件(有关详细信息,请参阅here)。我建议每次触发事件时都检查IsBusy属性,如果是,则表示页面已完全加载。

答案 7 :(得分:0)

将DocumentCompleted事件与包含多个嵌套帧的页面一起使用对我来说不起作用。

我使用Interop.SHDocVW库来构建WebBrowser控件,如下所示:

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's the main frame who called the event.
        if (pDisp == _webBrowserControl.ActiveXInstance)
            _complete = true;
    }

当使用webBrowserControl.Navigate(url)方法导航到定义的URL时,此代码适用于我,但是当使用htmlElement.InvokeMember(“click”单击html按钮时,我不知道如何控制页面完成)。

答案 8 :(得分:0)

您可以使用事件ProgressChanged;它最后一次被引发将表明文档已完全呈现:

this.webBrowser.ProgressChanged += new
WebBrowserProgressChangedEventHandler(webBrowser_ProgressChanged);
相关问题