Jamshaid K.
Jamshaid K.

Reputation: 4547

Finding an Html Control in c# winform by a webbrowser control

I am writing a Winform application that will navigate a web browser control to a specific URL as:

webBrowser1.Navigate("web.facebook.com");

And now I want to get all the HTML elements of this page and retrieve values of these controls, after googling and all the stuff I have managed to write this code snippet which shows no error but throws an exception saying:

Object Reference not set to an instance of object.

mshtml.IHTMLInputElement email = (mshtml.IHTMLInputElement)webBrowser1.Document.GetElementById("email").DomElement;
MessageBox.Show(email.value);

this code snippet is placed in the Navigated event of the web browser control. so how am I gonna do that? any Ideas and suggestion are Appreciated. Thanks!

Upvotes: 0

Views: 1035

Answers (2)

Jamshaid K.
Jamshaid K.

Reputation: 4547

Thanks a lot guys, i figured the way out,, Here is how i did that!

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    try
    {
        mshtml.IHTMLInputElement email = (mshtml.IHTMLInputElement)webFacebook.Document.GetElementById("email").DomElement;
        MessageBox.Show(email.value);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Special Thanks @Eser! :)

Upvotes: 0

Jens Meinecke
Jens Meinecke

Reputation: 2940

Put a breakpoint on the first assignment and figure out which bit is returning a null value:

  • webBrowser1.Document,
  • webBrowser1.Document.GetElementById("email") or
  • webBrowser1.Document.GetElementById("email").DomElement

so do something like this:

var doc = webBrowser1.Document;
var elem = doc.GetElementById("email");
var email = elem.DomElement;

and step through the code.

It might also be worth your while to browse the webBrowser1.Document in the debugger to see that it actually contains what you think it should contain.

Upvotes: 2

Related Questions