How to set value for checkbox via EmbeddedWB.FillForm ? (Delphi)

2.1k views Asked by At

how can i set value for a checkbox via FillForm method ? I tried these but doesn't work :

  W.FillForm('Chkname', 'True');
  W.FillForm('Chkname', '1');
  W.FillForm('Chkname', '', 1);
1

There are 1 answers

1
TLama On

Quite late, I know, but I'll try to answer this since it's a good question and since even the current version of the TEmbeddedWB doesn't have this feature implemented.

However you can add your own function for doing this; in the following example I'm using the interposed class of TEmbeddedWB where I overloaded the FillForm function with the version which supports check box and radio button filling.

If you would like to set the check box or select some radio button call this version of function, where:

  • FieldName (string) - is the name of the element
  • Value (string) - value of the element (can be empty, but in that case the first element of the FieldName will be set; web developers should use name value pairs IMHO)
  • Select (Boolean) - if True, check box is checked or radio button selected

Here is the code:

uses
  EmbeddedWB, MSHTML;

type
  TEmbeddedWB = class(EmbeddedWB.TEmbeddedWB)
  public
    function FillForm(const FieldName, Value: string;
      Select: Boolean): Boolean; overload;
  end;

implementation

function TEmbeddedWB.FillForm(const FieldName, Value: string;
  Select: Boolean): Boolean;
var
  I: Integer;
  Element: IHTMLElement;
  InputElement: IHTMLInputElement;
  ElementCollection: IHTMLElementCollection;
begin
  Result := False;
  ElementCollection := (Document as IHTMLDocument3).getElementsByName(FieldName);
  if Assigned(ElementCollection) then
    for I := 0 to ElementCollection.length - 1 do
    begin
      Element := ElementCollection.item(I, '') as IHTMLElement;
      if Assigned(Element) then
      begin
        if UpperCase(Element.tagName) = 'INPUT' then
        begin
          InputElement := (Element as IHTMLInputElement);
          if ((InputElement.type_ = 'checkbox') or (InputElement.type_ = 'radio')) and
            ((Value = '') or (InputElement.value = Value)) then
          begin
            Result := True;
            InputElement.checked := Select;
            Break;
          end;
        end;
      end;
    end;
end;

And here a basic example of usage:

procedure TForm1.Button1Click(Sender: TObject);
var
  WebBrowser: TEmbeddedWB;
begin
  WebBrowser := TEmbeddedWB.Create(Self);
  WebBrowser.Parent := Self;
  WebBrowser.Align := alClient;
  WebBrowser.Navigate('http://www.w3schools.com/html/html_forms.asp');

  if WebBrowser.WaitWhileBusy(15000) then
  begin
    if not WebBrowser.FillForm('sex', 'male', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Bike', True) then
      ShowMessage('Error while form filling occured...');
    if not WebBrowser.FillForm('vehicle', 'Car', True) then
      ShowMessage('Error while form filling occured...');
  end;
end;