OK,你需要使用DHTML来遍历页面中的所有元素(在DHTML中所有的东西都是元素,文本框是、超链接是、一段文本也是)。获得你所需要的元素再获得里面的值。下面是一个访问CSDN并填充用户名以及密码文本框的代码:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MSHTML, OleCtrls, SHDocVw, StdCtrls;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
Webbrowser1.Navigate('http://www.csdn.net');
end;procedure TForm1.Button2Click(Sender: TObject);
var
FDoc:IHTMLDocument2;
FItem:IHTMLElement;
FInput:IHTMLInputElement;
FImg:IHTMLInputImage;
vDoc,vTag:OLEVariant;
i:integer;
begin
FDoc:=Webbrowser1.Document as IHTMLDocument2;
if assigned(FDoc)then
begin
for i:=0 to FDoc.all.length -1 do
begin
FItem:=FDoc.all.item(i,i)as IHTMLElement;
if LowerCase(FItem.tagName)='input' then
begin
FInput:= FItem as IHTMLInputElement;
ListBox1.Items.Add(FInput.type_);
if (FInput.type_='text')or (FInput.type_='password')then
begin
if FInput.name = 'name'then
FInput.value := 'technofantasy'
else if FInput.name ='pass' then
FInput.value := 'hello'; //不是真正的密码哦 :-)
end
else if (FInput.type_='image')then
fImg:=FInput as IHTMLInputImage;
end;
end;
end;
end;end.
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MSHTML, OleCtrls, SHDocVw, StdCtrls;type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
Webbrowser1.Navigate('http://www.csdn.net');
end;procedure TForm1.Button2Click(Sender: TObject);
var
FDoc:IHTMLDocument2;
FItem:IHTMLElement;
FInput:IHTMLInputElement;
FImg:IHTMLInputImage;
vDoc,vTag:OLEVariant;
i:integer;
begin
FDoc:=Webbrowser1.Document as IHTMLDocument2;
if assigned(FDoc)then
begin
for i:=0 to FDoc.all.length -1 do
begin
FItem:=FDoc.all.item(i,i)as IHTMLElement;
if LowerCase(FItem.tagName)='input' then
begin
FInput:= FItem as IHTMLInputElement;
ListBox1.Items.Add(FInput.type_);
if (FInput.type_='text')or (FInput.type_='password')then
begin
if FInput.name = 'name'then
FInput.value := 'technofantasy'
else if FInput.name ='pass' then
FInput.value := 'hello'; //不是真正的密码哦 :-)
end
else if (FInput.type_='image')then
fImg:=FInput as IHTMLInputImage;
end;
end;
end;
end;end.
解决方案 »
- 请问:文字怎么显示不全?
- 如何检测用户多长时间没有鼠标与键盘操作?
- 怎样使ListView中的图标显示为gif类型的图片
- delphi更新ACCESS数据表出错!!(在线等待)
- Rave报表打印控件使用报错误问题
- 过年了,分数太多,散点分之7
- 报表中1行如何显示多条记录
- 各位老大帮我看看
- (北京)大量旧书出售,包括vb,vc,java,c#,delphi,linux,unix,ccna,sql service,oracle等计算机书,另外还有一些英语方面的比如新概念英语
- 关于locate语句的小问题!
- 如何实现在Form/Image等控件上拖动时候会出现一个虚框?然后出现一个控件???
- list index out of bounds(0)是什么意思?请教高手!!
这段程序是针对span或者div来类型来写的……参数Name就是span或者div的ID……
如果你要访问其他的类型,那就自己改一下,我不贴了……function WebElementTextPut(var WebBrowser:TWebBrowser;Name:AnsiString;Value:AnsiString) : HRESULT;
var spDisp : IDispatch;
IWB : IWebBrowser2;
IDoc : IHTMLDocument2;
IElc : IHTMLElementCollection;
ITae : IHTMLElement;
begin
if (WebBrowser.Document=Nil) then
begin
Result:=S_FALSE;
Exit;
end;
WebBrowser.Document.QueryInterface(IID_IHTMLDocument2,IDoc);
if (IDoc=Nil) then
begin
Result:=S_FALSE;
Exit;
end;
IElc:=IDoc.all;
if (IElc=Nil) then
begin
Result:=S_FALSE;
Exit;
end;
spDisp:=IElc.item(Name,0);
if (spDisp=Nil) then
begin
Result:=S_FALSE;
Exit;
end;
spDisp.QueryInterface(IID_IHTMLElement,ITae);
if (ITae=Nil) then
begin
Result:=S_FALSE;
Exit;
end;
ITae.innerText:=Value;
Result := S_OK;
end;