1. Computing

Populate and Submit Web Forms using the TWebBrowser

Manipulate (Fill) HTML Input Elements on a Web Form and Submit

By

Web pages can host web forms with input elements like edit boxes and drop down lists which you can control and manipulate programmatically from Delphi code.

Once you have the web form, you can list all the html input elements by their name:

 function WebFormFields(const document: IHTMLDocument2; const formName : string): TStringList;
 var
   form : IHTMLFormElement;
   field : IHTMLElement;
   fName : string;
   idx : integer;
 begin
   form := WebFormGet(0, WebBrowser1.Document AS IHTMLDocument2) ;
 
   result := TStringList.Create;
   for idx := 0 to -1 + form.length do
   begin
     field := form.item(idx, '') as IHTMLElement;
 
     if field = nil then Continue;
     fName := field.id;
 
     if field.tagName = 'INPUT' then fName := (field as IHTMLInputElement).name;
     if field.tagName = 'SELECT' then fName := (field as IHTMLSelectElement).name;
     if field.tagName = 'TEXTAREA' then fName := (field as IHTMLTextAreaElement).name;
 
     result.Add(fName) ;
   end;
 end; 

When you know the names of the fields on a web form, you can programmatically get the value for a single html field:

 function WebFormFieldValue(
   const document: IHTMLDocument2;
   const formNumber : integer;
   const fieldName : string): string;
 var
   form : IHTMLFormElement;
   field: IHTMLElement;
 begin
   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;
   field := form.Item(fieldName,'') as IHTMLElement;
   if field = nil then Exit;
 
   if field.tagName = 'INPUT' then result := (field as IHTMLInputElement).value;
   if field.tagName = 'SELECT' then result := (field as IHTMLSelectElement).value;
   if field.tagName = 'TEXTAREA' then result := (field as IHTMLTextAreaElement).value;
 end; 
An example of usage to get the value of an input field named "URL":
 const
   FIELDNAME = 'url';
 var
   doc :IHTMLDocument2;
   fieldValue : string;
 begin
   doc := WebBrowser1.Document AS IHTMLDocument2;
   fieldValue := WebFormFieldValue(doc, 0, FIELDNAME) ;
   memo1.Lines.Add('Field : "URL", value:' + fieldValue) ;
 end; 
The entire idea would have no value if you would not be able to fill in web form elements:
 procedure WebFormSetFieldValue(const document: IHTMLDocument2; const formNumber: integer; const fieldName, newValue: string) ;
 var
   form : IHTMLFormElement;
   field: IHTMLElement;
 begin
   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;
   field := form.Item(fieldName,'') as IHTMLElement;
   if field = nil then Exit;
 
   if field.tagName = 'INPUT' then (field as IHTMLInputElement).value := newValue;
   if field.tagName = 'SELECT' then (field as IHTMLSelectElement) := newValue;
   if field.tagName = 'TEXTAREA' then (field as IHTMLTextAreaElement) := newValue;
 end; 

Sumbit a Web Form

Finally, when all the fields are manipulated, you would probably want to submit the web form from Delphi code. Here's how:
 procedure WebFormSubmit(
   const document: IHTMLDocument2;
   const formNumber: integer) ;
 var
   form : IHTMLFormElement;
   field: IHTMLElement;
 begin
   form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;
 
   form.submit;
 end; 
Hm, the last one was obvious :)

Not all Web Forms are "Open Minded"

Some web forms might host a captcha image to prevent web pages from being manipulated programmatically.

Some web forms might not be submitted when you "click the submit button" - some web forms execute JavaScript or some other procedure gets executed handled by the "onsubmit" event of the web form.

In any way, web pages can be controlled programmatically, the only question is "how far are you prepared to go" :))

©2014 About.com. All rights reserved.