html 自动填表,Delphi WEB网页自动填表

Delphi中利用webbrowser控件来实现自动填表,此例为一模板,稍作修改可用来自动申请QQ、邮箱、论坛ID之类(不包含验证码识别)。

代码如下:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,

Forms,

Dialogs, StdCtrls,MSHTML, SHDOCVW,IdGlobal;

type

TMainFrm = class(TForm)

btnTest: TButton;

edURL: TEdit;

Label1: TLabel;

procedure btnTestClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

MainFrm: TMainFrm;

implementation

{$R *.dfm}

procedure FillIEForm(aURL:string);

procedure

DoWithHtmlElement(aElementCollection:IHTMLElementCollection);

var

k:integer;

vk:oleVariant;

Dispatch: IDispatch;

HTMLInputElement:IHTMLInputElement;

HTMLSelectElement:IHTMLSelectElement;

HTMLOptionElement: IHTMLOptionElement;

HTMLTextAreaElement: IHTMLTextAreaElement;

HTMLFormElement:IHTMLFormElement;

HTMLOptionButtonElement:IHTMLOptionButtonElement;

begin

for k:=0 to aElementCollection.length -1 do

begin

Vk:=k;

Application.ProcessMessages;

Dispatch:=aElementCollection.item(Vk,0);

if

Succeeded(Dispatch.QueryInterface(IHTMLInputElement,HTMLInputElement))

then

begin

With HTMLInputElement do//单行文本

begin

if (UpperCase(Type_)='TEXT') or (UpperCase(Type_)='PASSWORD')

then

begin

value:='text';

end

else if (UpperCase(Type_)='CHECKBOX') then//复选框

begin

checked:=true;

end

else if (UpperCase(Type_)='RADIO') then//单选框

begin

checked :=true;

end;

end;

end

else if

Succeeded(Dispatch.QueryInterface(IHTMLSelectElement,HTMLSelectElement))

then

begin

With HTMLSelectElement do//下拉框

begin

selectedIndex :=1;

end;

end

else if

Succeeded(Dispatch.QueryInterface(IHTMLTEXTAreaElement,HTMLTextAreaElement))

then

begin

with HTMLTextAreaElement do//多行文本

begin

value :='textarea';

end;

end

else if

Succeeded(Dispatch.QueryInterface(IHTMLOptionElement,HTMLOptionElement))

then

begin

with HTMLOptionElement do//下拉选项

begin

//处理

end;

end

else if

SUCCEEDED(Dispatch.QueryInterface(IHTMLFormElement,HTMLFormElement))then

begin

with HTMLFormElement do//表单

begin

//处理

end;

end

else if

SUCCEEDED(Dispatch.QueryInterface(IHTMLOptionButtonElement,HTMLOptionButtonElement))then

begin

//不明

//处理

end

else

//showmessage('other');

;

end;

end;

var

ShellWindow: IShellWindows;

Web: IWebBrowser2;

Dispatch: IDispatch;

i,j:integer;

IEAddress:string;

HTMLDocument:IHTMLDocument2;

ElementCollection:IHTMLElementCollection;

FrameWindow:IHTMLWindow2;

Vi,Vj:OLEVariant;

HTMLFrameBase :IHTMLFrameBase ;

HTMLFrameElement:IHTMLFrameElement ;

HTMLIFrameElement:IHTMLIFrameElement;

begin

ShellWindow := CoShellWindows.Create;

for i:=0 to ShellWindow.Count -1 do

begin

Vi:=i;

Dispatch:=Shellwindows.item(Vi);

if Dispatch=nil then continue;

Dispatch.QueryInterface(IWebBrowser2,Web);

if Web<>nil then

begin

IEAddress:=Web.LocationURL;

if Pos(aURL,IEAddress)>0 then

begin

Web.Document.QueryInterface(IHTMLDocument2,HTMLDocument);

if HTMLDocument<>nil then

begin

if HTMLDocument.frames.length =0 then//无框架

begin

ElementCollection:=HTMLDocument.Get_All;

DoWithHtmlElement(ElementCollection);

end

else//有框架

begin

for j:=0 to HTMLDocument.frames.length -1 do

begin

Vj:=j;

Dispatch:=HTMLDocument.frames.item(Vj);

// if

Succeeded(Dispatch.QueryInterface(IHTMLFrameBase,HTMLFrameBase)

if Succeeded(Dispatch.QueryInterface(IHTMLWindow2,FrameWindow))

then

begin

// DoWithHtmlElement(FrameWindow.document.all);

end;

End;

end;

end;

end;

End;

end;

end;

procedure TMainFrm.btnTestClick(Sender: TObject);

begin

FillIEForm(edUrl.Text);

end;

end.

单个frames的输入

var o : Olevariant; begin //找到登录用户名的输入框 o :=

WebBrowser.OleObject.document.all.item('LoginUserID',0); o.value :=

'TEST';

//找到登录密码的输入框

o := WebBrowser.oleobject.document.all.item('LoginPassword',0);

o.value := 'TEST'

//第一个表单提交 WebBrowser.oleobject.document.Forms.Item(0,

0).submit;

{ //或者用指定表单名称提交 o

:=WebBrowser.oleobject.document.all.item('Login',0); o.Click;

//点击操作,对其它对象也可同样操作 }

end; 多个frames的输入,FrameIndex为Frame的序号

var o : Olevariant; begin //找到登录用户名的输入框 o :=

WebBrowser.oleobject.document.documentelement.document.frames.item(FrameIndex).document.all.item('LoginUserID',0);

o.value := 'TEST';

//找到登录密码的输入框 o :=

WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.all.item('LoginPassword',0);

o.value := 'TEST'

//第一个表单提交

WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.Forms.Item(0,

0).submit;

{ //或者用指定表单名称提交 o

:=WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex)..document.all.item('Login',0);

o.Click;

//点击操作,对其它对象也可同样操作 }

end;

网页的源码不能读取,可能由于不能读源码,所以也不能用IHTMLDocument2及IHTMLElementCollection;

WebBrowse.Document.All.Item('控件ID',0).Value := 'AAa';

之类的来提交表单。我在网上查了很多资料,也不能用。类似以下的:

var doc : IHTMLDocument2; all : IHTMLElementCollection; len,i : integer; item : OleVariant; HtmlInputEle : IHTMLInputElement; SubmitBtn : IHTMLButtonElement; spDisp : IDispatch; begin if WebBrowser1.Document <> nil then begin doc:=WebBrowser1.Document as IHTMLDocument2; all:=doc.all; len:=all.Length; for i:=0 to len-1 do begin spDisp:=all.item(i,varEmpty); if SUCCEEDED(spDisp.QueryInterface(IHTMLInputElement ,HtmlInputEle)) then begin if HTMlInputEle.name = 'UserID' then HtmlInputEle.value := 'test'; if HTMlInputEle.name = 'Passwd' then HtmlInputEle.value := 'test'; end; if SUCCEEDED(spDisp.QueryInterface(IHTMLButtonElement ,SubmitBtn)) then if SubmitBtn.name = 'submitButtonName' then SubmitBtn.click(); end; end; end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值