uses MSHTML;procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant); var vHTMLDocument2: IHTMLDocument2; vHTMLElement: IHTMLElement; vHTMLElementCollection: IHTMLElementCollection; begin if not Assigned(TWebBrowser(Sender).Document) then Exit; TWebBrowser(Sender).OnDocumentComplete := nil; // 只点一次 vHTMLDocument2 := TWebBrowser(Sender).Document as IHTMLDocument2; vHTMLElementCollection := vHTMLDocument2.body.all as IHTMLElementCollection; vHTMLElement := vHTMLElementCollection.item('q', 0) as IHTMLElement; if not Assigned(vHTMLElement) then Exit; (vHTMLElement as IHTMLInputElement).value := 'Delphi'; vHTMLElement := vHTMLElementCollection.item('btnG', 0) as IHTMLElement; if not Assigned(vHTMLElement) then Exit; vHTMLElement.click; end;procedure TForm1.FormCreate(Sender: TObject); begin WebBrowser1.Navigate('http://www.google.cn'); end;
const pDisp: IDispatch; var URL: OleVariant);
var
vHTMLDocument2: IHTMLDocument2;
vHTMLElement: IHTMLElement;
vHTMLElementCollection: IHTMLElementCollection;
begin
if not Assigned(TWebBrowser(Sender).Document) then Exit;
TWebBrowser(Sender).OnDocumentComplete := nil; // 只点一次
vHTMLDocument2 := TWebBrowser(Sender).Document as IHTMLDocument2;
vHTMLElementCollection := vHTMLDocument2.body.all as IHTMLElementCollection;
vHTMLElement := vHTMLElementCollection.item('q', 0) as IHTMLElement;
if not Assigned(vHTMLElement) then Exit;
(vHTMLElement as IHTMLInputElement).value := 'Delphi';
vHTMLElement := vHTMLElementCollection.item('btnG', 0) as IHTMLElement;
if not Assigned(vHTMLElement) then Exit;
vHTMLElement.click;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.google.cn');
end;