delphi之模拟点击网页中的按钮

 

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Align := alTop;
  Button2.Enabled := False;
  Button3.Enabled := False;
  Button4.Enabled := False;
  Button1.Caption := '模拟一个页面';
  Button2.Caption := '假如知道按钮名称';
  Button3.Caption := '假如知道按钮的 ID';
  Button4.Caption := '假如只知道是第几个按钮';
end;

{模拟一个页面}
procedure TForm1.Button1Click(Sender: TObject);
begin
  WebBrowser1.Navigate(
    'about:<head><title>标题</title><body>' +
    '<form method="POST" action="'">http://www.nobird.org/">' +
    '<input type="submit" value="提交" id="btnID" name="btnName">' +
    '</form></body>');
  Button2.Enabled := True;
  Button3.Enabled := True;
  Button4.Enabled := True;
end;

{假如知道按钮名称, 譬如是: btnName}
procedure TForm1.Button2Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.all.item('btnName').click;
  //WebBrowser1.OleObject.document.all.item('btnName', 0).click;
  FormCreate(nil);
end;

{假如知道按钮的 ID, 譬如是: btnID}
procedure TForm1.Button3Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.getElementByID('btnID').click;
  FormCreate(nil);
end;

{假如只知道是第几个按钮, 譬如是第一个}
procedure TForm1.Button4Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.getElementsByTagName('input').item(0).click;
  FormCreate(nil);
end;

end.

你可能感兴趣的:(input,action,button,Delphi,WebBrowser,Forms)