C# know when WebBrowser Download is complete – Inline

I wanted to know exactly when a web page has downloaded into the WebBrowser.

By using the DocumentCompleted event I was able flag that it has been downloaded.

private void ConfirmWebPageDownloadComplete(string address)
{
    var webBrowser = new WebBrowser { ScriptErrorsSuppressed = true, };
    webBrowser.Navigate(address);
    var documentComplete = false;
    webBrowser.DocumentCompleted += (s, ea) => { documentComplete = true; };
    while (!documentComplete)
    {
        Application.DoEvents();
    }
    // Document has completed
    var html = webBrowser.Document.Body.Parent.OuterHtml;
}

Setting ScriptErrorsSuppressed = true is needed otherwise if there is a Javascript error it will display a message.

By using webBrowser.Document.Body.Parent.OuterHtml it gives the Html for the page.

Comments are closed.