Register  Login [ Home ]  Search
 Navigation
 .Net Preacher Show - Web Automation - Programmatically Submitting Forms and Filling Text Inputs

Web Automation - Programmatically Submitting Forms and Filling Text Inputs

Web Automation - Programmatically Submitting Forms and Filling Text Inputs

I regularly get emails about how to do web automation or browser automation in .NET. this is all despite (and largely because of) a previous tutorial and sample application I wrote on the subject that walks through how to automate a web browser in .NET 1.1.

So in this .NET Preacher Show we go over a few different ways to help automate a web browser control in .NET 2.0 with three ways to programmatically submit a form. Hopefully this walk through in the podcast will help people out more as there's a bit more explanation on web automation in here.

There are a few things in the sample application that illustrate side-effects of clicking the form submit button, so make sure to download it and check those out.

A lot of things are pretty trivial to do, but perhaps the most common thing needed is to programmatically click the submit button on a form. There are a few ways to do it, so here is the 'take-away' code from the sample application for programmatically clicking a submit button.

The three methods for programmatically clicking a web form submit button are:

   1:// Submit Web Forms Method #1 - click submit button method 1
   2:// This method is very reliable.
   3:HtmlElement el = wbrBrowser.Document.All["s1"];
   4:object obj = el.DomElement;
   5:System.Reflection.MethodInfo mi = obj.GetType().GetMethod("click");
   6:mi.Invoke(obj, new object[0]);
   7: 
   8:// Submit Web Forms Method #2 - click submit button method 2
   9:// This method sometimes fails if a form loses focus.
  10:HtmlDocument doc = wbrBrowser.Document;
  11:doc.GetElementById("s1").Focus();
  12:SendKeys.Send("{ENTER}");
  13: 
  14:// Submit Web Forms Method #3 - click submit button method 3
  15:// Another way to submit a form.
  16:HtmlElementCollection elcol = wbrBrowser.Document.All.GetElementsByName("WebAutomation");
  17:foreach (HtmlElement el in elcol)
  18:{
  19:if (el.TagName.ToLower().Equals("form"))
  20:    {
  21:        el.InvokeMember("submit");
  22:break;
  23:    }
  24:}

 We also quickly go over how to programmatically fill in text in a form input. It's very simple, and here's how to do that:

   1:// When you know the ID or name of an element, you have complete control of it. 
   2:// Here we simply change the value of the input.
   3:HtmlDocument doc = wbrBrowser.Document;
   4:doc.GetElementById("t1").InnerText = this.btnFill1.Text;

Of course the text that you fill could be anything you want on the right side of the equals sign.

Download the sample application at http://www.dotnetpreacher.com/.

 

Intro Music: Shit Out Of Luck

By: Straightfork

Being forced to use a PHP script, I needed to setup a couple WIMP servers (Windows IIS MySQL PHP) and really, truly felt SOL (not SQL) as getting the 'cross-platform' script to work took a lot more effort than simply installing it. In any event, this show's intro music reflects that. You can find Straightfork here.

 

Submitted by DotNetPreacher | 12:00 AM 9/8/2007 | Category .NET | Download

Back


  
Home | Code Samples | Forums
Thursday, March 11, 2010   Copyright 2007 by The Dot Net Preacher   Terms Of Use  Privacy Statement