测试例子: 外网电脑D:\TEST\delphiTest\webbrowsetest 参考文档: delphi 操作WebBrowser 元素值 http://hi.baidu.com/kinglike/item/c4608a8e55313f874514cf5c delphi中WEBBrowser网页JS函数调用delphi函数 http://blog.csdn.net/iseekcode/article/details/4740269 webbrowser精华内容 http://blog.csdn.net/iseekcode/article/details/4740367 delphi 向webbrowser打开的网页中插入js命令 http://blog.csdn.net/iseekcode/article/details/4740286
1.获取WEB控件值 function TForm1.fgetValue(editName: string): string; var i:integer; Doc:IHTMLDocument2; input:OleVariant; userinputelement,pwdinputelement,ValidateElement:ihtmlinputelement; ValidateImage: IHTMLImgElement; imagecount:integer; form:ihtmlformelement; myitem:Olevariant; begin Doc:=WebBrowser1.document as ihtmldocument2; if doc=nil then exit; // 第一种方式 userinputelement:=(doc.all.item(editName,0) as ihtmlinputelement); Result := userinputelement.value; end; 2.设置WEB控件值 procedure TForm1.setValueClick(Sender: TObject); var i:integer; Doc:IHTMLDocument2; input:OleVariant; userinputelement,pwdinputelement,ValidateElement:ihtmlinputelement; ValidateImage: IHTMLImgElement; imagecount:integer; form:ihtmlformelement; myitem:Olevariant; begin Doc:=WebBrowser1.document as ihtmldocument2; if doc=nil then exit; // 第一种方式 userinputelement:=(doc.all.item('userName',0) as ihtmlinputelement); userinputelement.value:='ADMIN'; userinputelement:=(doc.all.item('password',0) as ihtmlinputelement); userinputelement.value:='1'; end; 3.WEB调用DELPHI procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch; const URL, Flags, TargetFrameName, PostData, Headers: OleVariant; var Cancel: WordBool); begin // showmessage(url); if pos('qwbbLocatePage.action', url) > 0 then begin ShowMessage(fgetValue('userName')); Cancel:= true; end; end;
|
请发表评论