delphi之模拟点击网页中的按钮
有三种方法来提交
1、假如知道按钮名称
上面代码中有 这样的代码name=”btnLogin” ,表明此按钮名称为 btnlogin
程序代码就可以 这样来模拟
wb1.OleObject.document.all.item(‘btnLogin’).click;
2、假如知道按钮id
上面代码中有 这样的代码 id=”btnLogin” ,表明此按钮id为 btnlogin
程序代码可以 这样来模拟
wb1.OleObject.document.getElementByIdx_x(‘btnLogin’).click ;
3、假如名称及ID都没有,就只能判断这个提交按钮是第几个了]
按照登录页面源码来分析应当是第八个按钮,按以下语句即可实现登录按钮点击事件.
wb1.OleObject.document.getElementsByTagName_r(‘input’).item(7).click;
procedure TForm1.btn1Click(Sender: TObject);
begin
wb1.Navigate('http://passport.cnblogs.com/login.aspx');
end;
procedure TForm1.btnloginClick(Sender: TObject);
var
Doc: IHTMLDocument2;
userinputelement, pwdinputelement: ihtmlinputelement;
begin
doc := wb1.document as ihtmldocument2;
userinputelement := (doc.all.item('tbUserName', 0) as ihtmlinputelement);
userinputelement.value := Trim(edt1.Text);
pwdinputelement := (doc.all.item('tbPassword', 0) as ihtmlinputelement);
pwdinputelement.value := Trim(edt2.Text);
下面三种都可以,自己测试
a.按按钮名称来点击
wb1.OleObject.document.all.item('btnLogin').click;
b.按钮钮ID来点击
wb1.OleObject.document.getElementByIdx_x('btnLogin').click ;
c.按第几个按钮来点击
wb1.OleObject.document.getElementsByTagName_r('input').item(7).click;
end;
最后一种,没有ID ,name,也不知道Tag值,点击指定文字超链接可以通过下面方法:
memo1.Clear;
for i := 0 to WebBrowser1.OleObject.document.links.Length - 1 do
begin
memo1.Lines.add(WebBrowser1.OleObject.document.links.item(i).href); //链接
memo1.Lines.add(WebBrowser1.OleObject.document.links.item(i).innerText) //链接名称
end;
procedure TForm1.Button2Click(Sender: TObject);
var
doc:IHTMLDocument2;
all:IHTMLElementCollection;
i,sum:integer;
item:OleVariant;
begin
doc:=self.WebBrowser1.Document as IHTMLDocument2;
all:=doc.links;
sum:=all.length;
memo1.Clear;
for i:=0 to sum-1 do
begin
item:=all.item(i,varEmpty);
memo1.Lines.Add(item.href); //取链接(URL)
memo1.Lines.Add(item.innerText); //取链接名称
memo1.Lines.Add(item.outerHTML); //取链接源代码
memo1.Lines.Add(item.id); //链接ID
memo1.Lines.Add(item.tagName); //链接标签名
end;
procedure TForm1.Button1Click(Sender: TObject);
var
doc: IHTMLDocument2;
all: IHTMLElementCollection;
i, sum: integer;
item: OleVariant;
begin
doc := self.WebBrowser1.Document as IHTMLDocument2;
all := doc.links;
sum := all.length;
for i:=0 to sum-1 do
begin
item := all.item(i,varEmpty);
if item.innerText = '工商银行' then //www.hao123.com
item.click;
//WebBrowser2.Navigate(item.href);
end;
end;
类似的可以看这篇文章:[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;