0

I'm trying to login twitter mobile version in c# using getelementbyid. but when i invoke the click of the login button i get the following error. " Object reference not set to an instance of an object." here is the code im using. any help would be amazing.

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Document.GetElementById("session[username_or_email]").InnerText = textBox1.Text.ToString();
    webBrowser1.Document.GetElementById("session[password]").InnerText = textBox2.Text.ToString();
    webBrowser1.Document.GetElementById("signupbutton").InvokeMember("click");
}
Termininja
  • 6,620
  • 12
  • 48
  • 49
Waypast
  • 65
  • 1
  • 1
  • 9

1 Answers1

0

Ensure that the Document has fully loaded first, so make use of the the DocumentCompleted event available to the WebBrowser.

Add the DocumentCompleted event handler somewhere (in the constructor or form load, depending on the nature of your project)

private void Form_Load(object sender, EventArgs e) 
{
    //An example place to add this event handler.
    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Document_Complete);
}

private void Document_Complete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
   webBrowser1.Document.GetElementById("session[username_or_email]").InnerText = textBox1.Text;
   webBrowser1.Document.GetElementById("session[password]").InnerText = textBox2.Text;
   webBrowser1.Document.GetElementById("signupbutton").InvokeMember("click");
}

DocumentCompleted can fire many times, in which case see the answer in this post to help.

DocumentComplete documentation

Aside, there is no need to call .ToString() on the TextBoxes Text property, it's already a string.

Community
  • 1
  • 1
TEK
  • 1,265
  • 1
  • 15
  • 30