System.NullReferenceException with WebBrowser.Document.GetElementById()?

Category: c# general


Aoentoro on Sat, 01 Jun 2013 14:32:55

wb.Navigate(""); Thread.Sleep(9000); HtmlElement he = wb.Document.GetElementById("example_button"); he.InvokeMember("Click");

There is something wrong with the line 3 saying "System.NullReferenceException". I did some research and found out that it was mostly that the id didn't exist, but I've checked the web page before and it does contain the id. I don't know if this is a ridiculous question since I'm only a beginner, but please everybody including the experts help me find what's going wrong.


Family Tree Mike on Sat, 01 Jun 2013 15:29:07

The most likely reason the error occurs is that the id is not found.  Your code is likely to fail as you never handle the document completed event from the browser.  You are blocking the webbrowser from navigating with the sleep (I believe...).  Put the document manipulation code into the document completed event handler, including a check of the ReadyState property, and I think you will have success:


SundaramR on Tue, 04 Jun 2013 05:07:31


Yes! It seems the control is not loaded.. Have u checked document is loaded fully?

Where are you placing the code... Make sure the controls are loaded before you call this function...