{如果不用TWebBrowser...... 下面是从网上找到的aiirii曾引用过的一段代码,已测试可用 }uses MsHtml;procedure TForm1.Button1Click(Sender: TObject); var ShellWindow: IShellWindows; WB: IWebbrowser2; spDisp: IDispatch; IDoc1: IHTMLDocument2; Document: Variant; i, k, m: Integer; ovElements: OleVariant; begin ShellWindow := CoShellWindows.Create; for k := 0 to ShellWindow.Count do begin spDisp := ShellWindow.Item(k); if spDisp = nil then Continue; spDisp.QueryInterface(iWebBrowser2, WB); if WB <> nil then begin WB.Document.QueryInterface(IHTMLDocument2, iDoc1); if iDoc1 <> nil then begin WB := ShellWindow.Item(k) as IWebbrowser2; Document := WB.Document; for m := 0 to Document.forms.Length - 1 do begin ovElements := Document.forms.Item(m).elements; for i := 0 to ovElements.Length - 1 do begin try if (CompareText(ovElements.item(i).tagName, 'input') = 0) and (CompareText(ovElements.item(i).type, 'text') = 0) then ovElements.item(i).Value := '填充内容'; except end; (* try if (CompareText(ovElements.item(i).tagName, 'input') = 0) and (CompareText(ovElements.item(i).type, 'submit') = 0) then ovElements.item(i).Click; except end; *) end; end; end; end; end; end;
begin
Self.WebBrowser1.Navigate('c:\test.htm');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('表单名字',0).submit; {提交}
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('输入框名字',0).value := Self.Edit1.Text; {填充}
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('普通按钮名字',0).click; {点击}
end;
<html>
<head>
<title>test</title>
</head>
<body>
<form name="F1" method="POST" action="c:\test.htm">
<p><input type="text" name="T1" size="20">
<input type="button" value="按钮" name="B3" onclick="alert('B3 be Clicked!')">
<input type="submit" value="提交" name="B1">
<input type="reset" value="全部重写" name="B2">
</p>
</form>
</body>
</html>procedure TForm1.Button1Click(Sender: TObject);
begin
Self.WebBrowser1.Navigate('c:\test.htm');
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('F1',0).submit; {提交}
end;procedure TForm1.Button3Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('T1',0).value := Self.Edit1.Text; {填充}
end;procedure TForm1.Button4Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('B3',0).click; {点击}
end;procedure TForm1.Button5Click(Sender: TObject);
begin
Self.WebBrowser1.OleObject.document.all.item('F1',0).reset; {重置}
end;
下面是从网上找到的aiirii曾引用过的一段代码,已测试可用
}uses MsHtml;procedure TForm1.Button1Click(Sender: TObject);
var
ShellWindow: IShellWindows;
WB: IWebbrowser2;
spDisp: IDispatch;
IDoc1: IHTMLDocument2;
Document: Variant;
i, k, m: Integer;
ovElements: OleVariant;
begin
ShellWindow := CoShellWindows.Create;
for k := 0 to ShellWindow.Count do begin
spDisp := ShellWindow.Item(k);
if spDisp = nil then Continue;
spDisp.QueryInterface(iWebBrowser2, WB);
if WB <> nil then begin
WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
if iDoc1 <> nil then begin
WB := ShellWindow.Item(k) as IWebbrowser2;
Document := WB.Document;
for m := 0 to Document.forms.Length - 1 do begin
ovElements := Document.forms.Item(m).elements;
for i := 0 to ovElements.Length - 1 do begin
try
if (CompareText(ovElements.item(i).tagName, 'input') = 0) and
(CompareText(ovElements.item(i).type, 'text') = 0) then
ovElements.item(i).Value := '填充内容';
except
end;
(*
try
if (CompareText(ovElements.item(i).tagName, 'input') = 0) and
(CompareText(ovElements.item(i).type, 'submit') = 0) then
ovElements.item(i).Click;
except
end;
*)
end;
end;
end;
end;
end;
end;