Tuesday, November 5, 2019

Submit Forms Using TWebBrowser in Delphi

Populate/Submit Forms Using TWebBrowser in Delphi 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. Heres 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, lets say theres 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. Heres 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; Heres 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) ;   Ã‚  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; Submit  a Web Form Finally, when all the fields are manipulated, you would probably want to submit the web form from Delphi code. Heres 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?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.