title: Winform App - Webpage Interaction
body: <p>Windows Form Application – Manipulating input-elements in <code>WinForm WebBrowser</code></p>
  
  <p>Although I am familiar with <code>HttpWebResponse</code>/<code>HttpWebRequest</code> to login to a website, I was trying it now via using the mshtml library and found some weird behavior and I would like to see if someone else might be able to help me out here..</p>
  
  <p>I have an HTML login page with a java backend with a <code>Username</code> field, a <code>Password</code> field and a <code>Button</code>.</p>
  
  <p>The logic is very basic, I have a built a winform app with a built in webbrowser. 
  At the <code>Document_Completed</code> event I use the following code to enter my settings and to click the button.</p>
  
  <pre><code>private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
  {
      if (webBrowser.Url.ToString() == @"MyWebPage/signin")            
      {
          HTMLDocument hdc = new HTMLDocumentClass();
          hdc = (HTMLDocument)webBrowser.Document.DomDocument;
  
          IHTMLElement elb = hdc.getElementById("login_button");
          IHTMLInputElement elu = (IHTMLInputElement)hdc.getElementById("username");
          IHTMLInputElement elp = (IHTMLInputElement)hdc.getElementById("password");
  
          try
          {
              elu.value = "MyID";
              elp.value = "MyPwd";
              elb.click();
          }
          catch { }
      }
  }
  </code></pre>
  
  <p>Apart for this code being very quick and without error handling, it should do the trick and it does, partially..</p>
  
  <p>There are two scenario's:</p>
  
  <ul>
  <li><p>I launch the tool, it loads the webpage.</p>
  
  <ul>
  <li>The tool populates the UserID field and the Password field correctly</li>
  <li>The tool fails to click the button</li>
  </ul></li>
  <li><p>I click the button manually, I am logged in, I click logout, I am back at login page</p>
  
  <ul>
  <li>I immediatly logged in again, the tool enters the information</li>
  <li>The tool immediatly clicks the button as well.</li>
  </ul></li>
  </ul>
  
  <p>Is there anyone who might be able to explain me why this happens and how I could get around this with the current setup (hence not using <code>HttpWebRequest</code>). I don't see the difference between loading the page at startup or being redirected after logout, but apparently there is a difference in there or I am doing something wrong.</p>
  
  <p>Any feedback on this matter is very much appreciated.</p>
  
  <p>Thanks,
  Kevin</p>
  
  <h2>EDIT:</h2>
  
  <p>I added a <code>Button</code> to my Windows Form that bas the same backend Code as below in order to click the button on the webpage, this works perfectly. </p>
  
  <p>I triggered clicking this button in the <code>webBrowser_Completed</code> event but it doesn't work.
  For some reason, everything I add to the <code>webBrowser_DocumentCompleted</code> event does not allow me to trigger the click event for the button in my <code>WebBrowser</code> control. Once that entire event has completed, if I then try to trigger it it works but I would like to automate this.. Any advice?</p>