wojciech_rak
wojciech_rak

Reputation: 2346

WebBrowser Document is always null

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?

Upvotes: 8

Views: 19475

Answers (2)

TheGateKeeper
TheGateKeeper

Reputation: 4520

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

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

Upvotes: 2

Andras Zoltan
Andras Zoltan

Reputation: 42333

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.

Upvotes: 15

Related Questions