Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 962
wojciech_rak Avatar asked Mar 29 '12 12:03

wojciech_rak


2 Answers

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.

like image 124
Andras Zoltan Avatar answered Oct 22 '22 13:10

Andras Zoltan


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

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

like image 39
TheGateKeeper Avatar answered Oct 22 '22 13:10

TheGateKeeper