懶得寫了﹐轉抄一段
主要是用 IShellWindows 接口來獲得 IE1的COM接口
然后用其 document 接口進行操作﹐兩個接口的說明可以看 MSDN
procedure TForm1.PutDataframe(iName,ss,sBtnName:String);
var
ShellWindow: IShellWindows;
nCount: integer;
spDisp: IDispatch;
i,j,k: integer;
vi,l,o: OleVariant;
IE1: IWebBrowser2;
IDoc1: IHTMLDocument2;
oneframe:Ihtmlwindow2;
S1,S2 : string;
HtmlInputEle : IHTMLInputElement;
begin
ShellWindow := CoShellWindows.Create;
nCount := ShellWindow.Count;
for i := 0 to nCount - 1 do
begin
vi := i;
spDisp := ShellWindow.Item(vi);
if spDisp = nil then continue;
spDisp.QueryInterface( iWebBrowser2, IE1 );
if IE1 <> nil then
begin
IE1.Document.QueryInterface(IHTMLDocument2,iDoc1);
if iDoc1 <> nil then
begin
if idoc1.frames.length<>0 then
for k:= 0 to idoc1.frames.length-1 do
begin
l:=k;
spDisp:=idoc1.frames.item(l);
if SUCCEEDED(spDisp.QueryInterface(IHTMLWindow2 ,oneframe))then
begin
for j:=0 to oneframe.document.all.length-1 do
begin
Application.ProcessMessages;
spDisp := oneframe.document.all.item(J, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLInputElement ,HtmlInputEle))then
with HtmlInputEle do
begin
S1:=Name;
S2:=Type_;
S2:=UpperCase(S2);
S1:=UpperCase(S1);
if (StrComp(PChar(S2),'TEXT')=0) or (StrComp(PChar(S2),'PASSWORD')=0) then
begin
if S1=UpperCase(iName) then
begin
value :=ss;
o:=oneframe.document.all.item(sBtnName,0);
o.click;
exit;
end;
end;
end;
end;
end;
end;
end;
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
putdataFrame('Words','嘻嘻嘻','submit');
//向名称为words的文本框内填入"嘻嘻嘻",并点击名称为"submit"的按钮
end;