WebBrowser Document is always null

17,922

Solution 1

You should handle the DocumentCompleted event and access the document in your event handler when that fires.

Navigation and document loading is handled asynchronously - therefore the control hasn't actually navigated or loaded anything when the Navigate method returns; hence why these are null.

Solution 2

It's always null because it hasen't loaded yet.

What you need to do is subscribe to the webBrowser.DocumentCompleted event.

Share:
17,922
wojciech_rak
Author by

wojciech_rak

Full time .NET/C# software engineer. After hours as open-minded technology enthusiast.

Updated on June 13, 2022

Comments

  • wojciech_rak
    wojciech_rak almost 2 years

    I have this piece of code:

    WebBrowser wb = new WebBrowser();
    wb.Navigate(URL);
    HtmlDocument doc = wb.Document;
    

    I should mention, that I have no WebBrowser Control on a form, it's just in method in my class. After this, wb.Document and doc as well are nulls. Why is that? What do I have to do to obtain this document?