我这里给一个执行按钮的Click事件的例子,我认为你稍微修改之即可达到要求。 //执行指定名称的Object的Click事件 procedure TFmRequest.ClickObject(const ObjName: string); var ObjHtml : IHtmlElement; CurDocument: IHTMLDocument2;//执行ExecuteTrans后取得的浏览器窗口接口 begin IsFinished := False; ObjHtml := HtmlObjs.Item(ObjName, 0) as IHTMLElement; if ObjHtml = nil then Exit;
ObjHtml.Click;//试试.Focus?用delphi的代码提示可以看到很多方法 while True do begin Application.ProcessMessages; if IsFinished then Break; end; WebBrowser.Document.QueryInterface(IHTMLDocument2, CurDocument); HtmlObjs := CurDocument.All; end;
procedure TForm1.Button1Click(Sender: TObject); var Doc: IHTMLDocument2; Form : IHTMLFormElement; Elements: IHTMLElementCollection; InputElem : IHTMLInputElement; i : Integer; begin WebBrowser1.Navigate('http://oakhome.xicp.net/search.htm'); while(WebBrowser1.Busy)do begin Application.ProcessMessages; end; Doc := WebBrowser1.Document as IHTMLDocument2; Elements := Doc.Forms as IHTMLElementCollection; Form := Elements.Item(0,varEmpty) as IHTMLFormElement; Elements := (Doc.All as IHTMLElementCollection).tags('input') as IHTMLElementCollection; for i := 0 to Elements.length-1 do begin InputElem := Elements.Item(i, varEmpty) as IHTMLInputElement; if UpperCase(Trim(InputElem.name)) = 'KEYS' then InputElem.select; end; end;
var form : IHTMLFormElement ; d:IHTMLDocument2 ; begin with WebBrowser1 do begin d := document as IHTMLDocument2; (form.item(0,0) as IHTMLElement2).focus; end;end;
setFocus;如使EDIT得到焦点:edit1.SetFocus;
读出html的源程序,添加相应代码。
试试看。
//执行指定名称的Object的Click事件
procedure TFmRequest.ClickObject(const ObjName: string);
var
ObjHtml : IHtmlElement;
CurDocument: IHTMLDocument2;//执行ExecuteTrans后取得的浏览器窗口接口
begin
IsFinished := False;
ObjHtml := HtmlObjs.Item(ObjName, 0) as IHTMLElement;
if ObjHtml = nil then Exit;
ObjHtml.Click;//试试.Focus?用delphi的代码提示可以看到很多方法
while True do
begin
Application.ProcessMessages;
if IsFinished then Break;
end; WebBrowser.Document.QueryInterface(IHTMLDocument2, CurDocument);
HtmlObjs := CurDocument.All;
end;
HtmlObjs: IHtmlElementCollection;
我在另一个地方对HtmlObjs赋值:
//运行应用服务器上的交易
procedure TFmRequest.ExecuteTrans(const TransUrl: string; const AutoLogin: Boolean);
var
ErrorStr: string;
CurDocument: IHTMLDocument2;//执行ExecuteTrans后取得的浏览器窗口接口
begin
..............
WebBrowser.Document.QueryInterface(IHTMLDocument2, CurDocument);
HtmlObjs := CurDocument.All;
.....
end;
IHTMLELEMENT没有SETFOCUS或FOCUS的属性或方法啊,我看过了,不信你试试哦
有人能解决吗,谢谢先。
var
Doc: IHTMLDocument2;
Form : IHTMLFormElement;
Elements: IHTMLElementCollection;
InputElem : IHTMLInputElement;
i : Integer;
begin
WebBrowser1.Navigate('http://oakhome.xicp.net/search.htm');
while(WebBrowser1.Busy)do
begin
Application.ProcessMessages;
end; Doc := WebBrowser1.Document as IHTMLDocument2;
Elements := Doc.Forms as IHTMLElementCollection;
Form := Elements.Item(0,varEmpty) as IHTMLFormElement;
Elements := (Doc.All as IHTMLElementCollection).tags('input') as IHTMLElementCollection;
for i := 0 to Elements.length-1 do
begin
InputElem := Elements.Item(i, varEmpty) as IHTMLInputElement; if UpperCase(Trim(InputElem.name)) = 'KEYS' then
InputElem.select;
end;
end;
d:IHTMLDocument2 ;
begin
with WebBrowser1 do begin
d := document as IHTMLDocument2;
(form.item(0,0) as IHTMLElement2).focus;
end;end;