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

How to set a value for a checkbox using the FillForm method? I tried them but did not work:

  W.FillForm('Chkname', 'True');
  W.FillForm('Chkname', '1');
  W.FillForm('Chkname', '', 1);
+3
source share
1 answer

Pretty late, I know, but I will try to answer this because it is a good question, and since even the current version has TEmbeddedWBnot implemented this feature.

However, you can add your own function for this; in the following example, I use the introduced class TEmbeddedWB, where I overloaded the function with a FillFormversion that supports the checkbox and populate the radio buttons.

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

  • FieldName (string) -
  • () - ( , FieldName, - IMHO)
  • (Boolean) - True,

:

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;

:

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;
+3

Source: https://habr.com/ru/post/1760629/


All Articles