html 自动填表,Delphi WEB网页自动填表
Delphi中利用webbrowser控件來實(shí)現(xiàn)自動(dòng)填表,此例為一模板,稍作修改可用來自動(dòng)申請QQ、郵箱、論壇ID之類(不包含驗(yàn)證碼識(shí)別)。
代碼如下:
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//復(fù)選框
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//下拉選項(xiàng)
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.
單個(gè)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'
//第一個(gè)表單提交 WebBrowser.oleobject.document.Forms.Item(0,
0).submit;
{ //或者用指定表單名稱提交 o
:=WebBrowser.oleobject.document.all.item('Login',0); o.Click;
//點(diǎn)擊操作,對其它對象也可同樣操作 }
end; 多個(gè)frames的輸入,FrameIndex為Frame的序號(hào)
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'
//第一個(gè)表單提交
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;
//點(diǎn)擊操作,對其它對象也可同樣操作 }
end;
網(wǎng)頁的源碼不能讀取,可能由于不能讀源碼,所以也不能用IHTMLDocument2及IHTMLElementCollection;
WebBrowse.Document.All.Item('控件ID',0).Value := 'AAa';
之類的來提交表單。我在網(wǎng)上查了很多資料,也不能用。類似以下的:
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;
總結(jié)
以上是生活随笔為你收集整理的html 自动填表,Delphi WEB网页自动填表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 酷狗歌曲资源最新版权保护和反爬机制
- 下一篇: MFC 画线,画刷,文本