Submit a Form in TWebbrowser with an image submit button

Spread the love

Submit a Form in TWebbrowser with an image submit button

If you have a webpage with a Form on it, but the submit – button is an image,
you can use this code (using a TWebBrowser)

Falls du eine Webpage mit einem Formular hast,
bei welchem der Submit-Knopf ein Bild ist,
dann kannst du diesen Code benutzen (sowie ein TWebBrowser)

uses
  MSHTML;

var
  iDoc: IHtmlDocument2;
  i: integer;
  ov: OleVariant;
  iDisp: IDispatch;
  iColl: IHTMLElementCollection;
  InputImage: HTMLInputImage;
begin
  WebBrowser1.ControlInterface.Document.QueryInterface(IHtmlDocument2, iDoc);
  if not Assigned(iDoc) then
  begin
    Exit;
  end;
  ov := 'INPUT';
  iDisp := iDoc.all.tags(ov);
  if Assigned(IDisp) then
  begin
    IDisp.QueryInterface(IHTMLElementCollection, iColl);
    if Assigned(iColl) then
    begin
      for i := 1 to iColl.Get_length do
      begin
        iDisp := iColl.item(pred(i), 0);
        iDisp.QueryInterface(HTMLInputImage, InputImage);
        if Assigned(InputImage) then
        begin
          if InputImage.Name = 'submit' then
          // if the name is submit / falls der name submit lautet
          begin
            InputImage.Click;  // click it / klick es
          end;
        end;
      end;
    end;
  end;
end;

// 2.

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Word;
  Document: IHtmlDocument2;
  str: string;
begin
  // Schleife über alle Bilder im Webbrowser
  for i := 0 to WebBrowser1.OleObject.Document.Images.Length - 1 do
  begin
    Document := WebBrowser1.Document as IHtmlDocument2;
    // URL auslesen
    Str := (Document.Images.Item(i, 0) as IHTMLImgElement).Href;
    // Dateiname des Bildes überprüfen
    if Pos('submit_icon.gif', str) <> 0 then
    begin
      ((Document.Images.Item(i, 0) as IHTMLImgElement) as IHTMLElement).Click;
    end;
  end;
end;