title: auto login on webbrowser control
body: <p>I am creating a form with a Webbrowser control that automatically logs in to a site.  When I debug it, I can see that it launches to the site and fill the username and password just fine, but once it's logged in, it's going through the same code again, hence causing error as it cannot find the same elements.  Why is the program looping through this code? Did I enter the code to the wrong event handler?</p>
  
  <pre><code>namespace MyProject
  {
  public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
          HtmlDocument doc = webBrowser1.Document;
          HtmlElement username = doc.GetElementById("UserName");
          HtmlElement password = doc.GetElementById("Password");
          HtmlElement submit = doc.GetElementById("submit");
          username.SetAttribute("value", "XXXXXXXX");
          password.SetAttribute("value", "YYYYYYYYYY");
          submit.InvokeMember("click");
      }
  }
  }
  </code></pre>