Fill HTML form via TWebBrowser in Delphi on Android

435 Views Asked by At

On a website's <form> I want to set the text of an <input> and submit that form using TWebBrowser. I want this for Delphi on Android similar to this answer, but that only works on Windows.

1

There are 1 best solutions below

0
Softacom On

The code you linked to works only on Windows, because it use VCL Webbrowser component that depends on Microsoft IE ActiveX and its engine (MSHTML) on which that code rely (by using components and interfaces such as IHTMLElementCollection).

For a cross-platform solution, if you are using FMX Webbrowser component or other third-party cross-platform webbrowser for Delphi, you can use its function for executing JavaScript ("EvaluateJavaScript" for FMX Webbrowser, or "ExecuteJavascript" if you are using TMSFNCWebBrowser from TMS Software) for the aim of editing and filling and even submitting the HTML form through JavaScript DOM codes.

It is an excellent cross-platform solution, you have to formulate a JavaScript code snippet and put it in the "EvaluateJavaScript", pay attention to the delimiters and the length of your string, like the following code for filling the field (with id "username") with the value "foo" and submitting the form (with id "myform") :

procedure FillForm;
begin
Webbrowser1.EvaluateJavaScript('document.getElementById(''username'').value="foo";' + 'document.getElementById(''myform'').submit();');
end;

Otherwise, if you are the developer of the website containing that form or you know how it is processed (action page, request type and parameters names), then you can simply send data through REST requests. Just use the REST Client Library and formulate a request to the action page then you implement the code for handling the received response.