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 do not have a WebBrowser control in the form, this is just a method in my class. After that, wb.Document and doc are also zeros. Why is this? What do I need to do to get this document?

+7
source share
2 answers

You must handle the DocumentCompleted event and access the document in the event handler when it fires.

Navigation and document loading are processed asynchronously - therefore, the control does not actually move or load when the Navigate method returns; therefore they are null.

+13
source

It is always null because it is not loaded yet.

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

+1
source

All Articles