Social Icons

Pages

Thursday, June 14, 2012

Auto click button and auto fill form- c#

This tutorial will show you how to auto fill forms and click buttons in a website using the web Browser control.
To show you how autoclick/autofill works we’ll make a simple login page.

So let’s begin…
First of all, add a webBrowser control to your form.
Then add a button control to your form and call the web page in the button_click event.

   private void btnSubmit_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate(URL);

        }
After that add DoucmentComplete event for the web Browser . DocumentComplete event is fired when the page is loaded completely.

Use this code:

private void webBrowserLogin_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {

 webBrowser1.Document.GetElementById("Username").SetAttribute("value", "TEST");
 webBrowser1.Document.GetElementById("password").SetAttribute("value", "TEST1234");
 webBrowser1.Document.GetElementById("Submit").InvokeMember("click");
}
Click the button using tag name and Attributename
HtmlElementCollection ElementCollection = webBrowserWCS.Document.GetElementsByTagName("input");
 foreach (HtmlElement curElement in ElementCollection)
   {
     string Attributename= "";
     iFoundIt = curElement.GetAttribute("value");
     if (Attributename == "Submit Details")
         {
          curElement.InvokeMember("click");
         }
   }