카테고리 없음

[COM/OLE] 특정 사이트의 form 을 강제 submit 하기

쇼핑스크래퍼3 2023. 9. 1. 08:42
// Google 사이트에서 'Delphi' 키워드로 검색하는 예제
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw, MSHTML, ShellAPI;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Webbrowser1.Navigate('http://www.google.co.kr');
   ShellExecute(GetDesktopWindow, 'open', 'http://www.google.co.kr', nil, nil, SW_SHOWNORMAL);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
begin
  ShellWindow := CoShellWindows.Create;
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        begin
          Document := WB.Document;
          for m := 0 to Document.forms.Length - 1 do
          begin
            ovElements := Document.forms.Item(m).elements;
            for i := 0 to ovElements.Length - 1 do
            begin
              try
                if (CompareText(ovElements.item(i).tagName, 'input') = 0) and
                   (CompareText(ovElements.item(i).name, 'q') = 0) then
                  ovElements.item(i).Value := 'Delphi';
              except
              end;
              try
                if (CompareText(ovElements.item(i).tagName, 'input') = 0) and
                   (CompareText(ovElements.item(i).type, 'submit') = 0) and
                   (ovElements.item(i).Value = '구글 검색') then
                  ovElements.item(i).Click;
              except
              end;
            end;
          end;
        end;
      end;
    end;
  end;
end;

end.