The TWebBrowser Delphi control provides access to the Web browser functionality from your Delphi apps - to allow you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications.
Web FormsA web form or a form on a web page allows a web page visitor to enter data that is, in most cases, sent to the server for processing.
A simplest web form could consist of one input element (edit control) and a submit button. Most web search engines (like Google) use such a web form to allow you to search the internet.
More complex web forms would include drop down lists, check boxes, radio buttons, etc. A web form is much like a standard windows form with text input and selection controls.
Every form would include a button - a submit button - a button that tells the browser to take action on the web form (typically to send it to a web server for processing).
Programmatically Populating Web FormsIf in your desktop application you use the TWebBrowser to display web pages - you can programmatically control web forms: manipulate, change, fill, populate fields of a web form and submit it.
Here's a collection of custom Delphi functions you can use to list all the web forms on a web page, to retrieve input elements, to programmatically populate fields and to finally submit the form.
To more easily follow the examples, let's say there's a TWebBrowser control named "WebBrowser1" on a Delphi (standard Windows) form.
Note: you should add mshtml to your uses clause in order to compile the methods listed here.
List Web Form Names, Get a Web Form by IndexA web page would in most cases have only one web form, but some web pages might have more than one web form. Here's how to get the names of all the web forms on a web page:
A simple usage to display the list of web form names in a TMemo:
function WebFormNames(const document: IHTMLDocument2): TStringList; var forms : IHTMLElementCollection; form : IHTMLFormElement; idx : integer; begin forms := document.Forms as IHTMLElementCollection; result := TStringList.Create; for idx := 0 to -1 + forms.length do begin form := forms.item(idx,0) as IHTMLFormElement; result.Add(form.name) ; end; end;
var forms : TStringList; begin forms := WebFormNames(WebBrowser1.Document AS IHTMLDocument2) ; try memo1.Lines.Assign(forms) ; finally forms.Free; end; end;
Here's how to get the instance of a web form by index - for a single form pages the index would be 0 (zero).
Once you have the web form, you can list all the html input elements by their name, you can get or set the value for each of the fields, and finally, you can submit the web form.
function WebFormGet(const formNumber: integer; const document: IHTMLDocument2): IHTMLFormElement; var forms : IHTMLElementCollection; begin forms := document.Forms as IHTMLElementCollection; result := forms.Item(formNumber,'') as IHTMLFormElement end;