unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls, MSHtml;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Ed_Keyword: TEdit;
    btnGo: TButton;
    WebBrowser1: TWebBrowser;
    procedure btnGoClick(Sender: TObject);
    procedure WebBrowser1DocumentComplete(ASender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnGoClick(Sender: TObject);
begin
   self.WebBrowser1.Navigate('http://www.google.com.tw/search?q='+self.Ed_Keyword.Text);
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
   HD : IHTMLDocument2;
   links : IHTMLElementCollection;
   linkItemCount : integer;
   linkItem : IHTMLLinkElement;
   item : IHTMLElement;
   count : integer;
   found : boolean;
begin
   //
   HD := self.WebBrowser1.Document as IHTMLDocument2;

   linkItemCount := HD.links.length;
   links := HD.links;
   found := false;
   count := 0;
   repeat
       item := links.item(count, 0) as IHTMLElement;
       //ShowMessage(item.tagName);

       found := item.tagName = 'A';
       ShowMessage(item.innerHTML);
       if found then begin
          self.WebBrowser1.OnDocumentComplete := nil;
          item.click;
       end;
       Inc(count);
   until found;
end;

end.
