Manipulate Web Forms Using the TWebBrowser

Web Forms and Web Element From a Delphi Perspective

Programming language
Getty Images/ermingut

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 Forms

A 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.

The 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 - that tells the browser to take action on the web form (typically to send it to a web server for processing).

Programmatically Populating Web Forms

If 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 Index

A 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:

 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;

A simple usage to display the list of web form names in a TMemo:

 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 page the index would be 0 (zero).

 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; 

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.

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) ;   endend;

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): stringvar   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 : stringbegin  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;

Submit 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;

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 event, web pages can be controlled programmatically, the only question is "how far are you prepared to go?"

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Manipulate Web Forms Using the TWebBrowser." ThoughtCo, Sep. 16, 2020, thoughtco.com/manipulate-web-forms-using-the-twebbrowser-1058362. Gajic, Zarko. (2020, September 16). Manipulate Web Forms Using the TWebBrowser. Retrieved from https://www.thoughtco.com/manipulate-web-forms-using-the-twebbrowser-1058362 Gajic, Zarko. "Manipulate Web Forms Using the TWebBrowser." ThoughtCo. https://www.thoughtco.com/manipulate-web-forms-using-the-twebbrowser-1058362 (accessed March 28, 2024).