首页 > 代码库 > delphi webbrowser 常用方法示例

delphi webbrowser 常用方法示例

var   Form   :     IHTMLFormElement     ;        D:IHTMLDocument2     ;begin    with   WebBrowser1   do   begin          D   :=   Document   as   IHTMLDocument2;          Form   :=   D.Forms.item( form1 ,0)   as   IHTMLFormElement;   //form1为表单名          //title为表单中的文本框        (form.item( title ,0)   as   IHTMLElement).setAttribute( value ,s_title,0);          (form.item( content ,0)   as   IHTMLElement).setAttribute( value ,edit1.text,0);        (form.item( add ,0)   as   IHTMLElement).click;//add为按钮名称    end;

在delphi的WebBrowser中获取和设置Input表单值

var    i:Integer;    myole:oleVariant;begin    myole := wb1.Document;    for i := 0 to myole.all.length - 1 do    begin        if myole.all.item(i).tagName = INPUT then        begin            mmo1.Lines.Add(myole.all.item(i).name);            mmo1.Lines.Add(myole.all.item(i).value);        end;    end;
end;
WebBrowser1.GoHome; //到浏览器默认主页WebBrowser1.Refresh; //刷新WebBrowser1.GoBack; //后退WebBrowser1.GoForward; //前进WebBrowser1.Navigate(...); //打开指定页面WebBrowser1.Navigate(about:blank); //打开空页面--------------------------------------------------------------------------------//打开空页面, 并写入...WebBrowser1.Navigate(about:<head><title>标题></title><body>页面内容</body>);--------------------------------------------------------------------------------//读取网页脚本中的变量:procedure TForm1.Button1Click(Sender: TObject);vars: string;i: Integer;begins := WebBrowser1.OleObject.document.Script.str;i := WebBrowser1.OleObject.document.Script.num;ShowMessage(s); //HelloShowMessage(IntToStr(i)); //99//也可以这样读:s := WebBrowser1.OleObject.document.parentWindow.str;i := WebBrowser1.OleObject.document.parentWindow.num;ShowMessage(s); //HelloShowMessage(IntToStr(i)); //99end;假如网页中有这样的语句:<script>varstr = "Hello";i = 99;</script>--------------------------------------------------------------------------------//调用网页脚本中的函数:procedure TForm1.Button1Click(Sender: TObject);beginWebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js//如需指定脚本语言, 需要:WebBrowser1.OleObject.document.parentWindow.execScript(MB(),JavaScript); //HTML-Jsend;假如有这样的脚本:<script>function MB(){alert(‘HTML-Js‘);}</script>--------------------------------------------------------------------------------//判断网页及内部框架网页是否全部下载完毕procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;const pDisp: IDispatch; var URL: OleVariant);beginif WebBrowser1.Application = pDisp thenbeginText := 网页下载完毕!;end;end;--------------------------------------------------------------------------------//改变背景色或背景图片:WebBrowser1.OleObject.document.body.bgcolor := #FF0000;WebBrowser1.OleObject.document.body.background := ...图片地址;--------------------------------------------------------------------------------//操作有 ID 标签的对象:vars: string;begins := WebBrowser1.OleObject.document.getElementByID(span1).innerText;ShowMessage(s); //这是 span1 标签中的内容//或者:s := WebBrowser1.OleObject.document.parentWindow.span1.innerText;ShowMessage(s); //这是 span1 标签中的内容//隐藏它:WebBrowser1.OleObject.document.parentWindow.span1.style.display := none;end;假如网页中有这样的内容:<span id=span1>这是 span1 标签中的内容</span>--------------------------------------------------------------------------------//获取网页源代码vars: string;begins := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码s := WebBrowser1.OleObject.document.body.outerHTML; //body内的所有代码, 包含body标签s := WebBrowser1.OleObject.document.documentElement.innerHTML; //html内的所有代码end;--------------------------------------------------------------------------------//WebBrowser 中的右键菜单//先要添加ApplicationEvents1,指定其Message事件//屏蔽右键菜单procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);beginwith Msg dobeginif not IsChild(WebBrowser1.Handle, hWnd) then Exit;Handled := (message = WM_RBUTTONDOWN) or (message = WM_RBUTTONUP) or (message = WM_CONTEXTMENU);end;end;//替换右键菜单procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);var mPoint: TPoint;beginif IsChild(WebBrowser1.Handle, Msg.Hwnd) and((Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)) thenbeginGetCursorPos(mPoint); //得到光标位置PopupMenu1.Popup(mPoint.X, mPoint.Y); //弹出popupmenu1的菜单Handled:=True;end;end;--------------------------------------------------------------------------------//新页面写入beginWebBrowser1.Navigate(about:blank);WebBrowser1.OleObject.Document.Writeln(ok);end; 

 

自动登录

单个frames的输入varo : Olevariant;begino := WebBrowser.OleObject.document.all.item(LoginUserID,0);    //找到登录用户名的输入框o.value := TEST;o := WebBrowser.oleobject.document.all.item(LoginPassword,0); //找到登录密码的输入框o.value := TESTWebBrowser.oleobject.document.Forms.Item(0, 0).submit;           //第一个表单提交{o :=WebBrowser.oleobject.document.all.item(‘Login‘,0);           //或者用指定表单名称提交o.Click;   //点击操作,对其它对象也可同样操作}end;多个frames的输入,FrameIndex为Frame的序号varo : Olevariant;begin//找到登录用户名的输入框o := WebBrowser.oleobject.document.documentelement.document.frames.item(FrameIndex).document.all.item(LoginUserID,0);o.value := TEST;//找到登录密码的输入框o := WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.all.item(LoginPassword,0);o.value := TEST//第一个表单提交WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex).document.Forms.Item(0, 0).submit;{//或者用指定表单名称提交o :=WebBrowser.oleobject.document.documentelement.document.frames.item(FramIndex)..document.all.item(‘Login‘,0);o.Click;    //点击操作,对其它对象也可同样操作}end; 

 

delphi webbrowser 常用方法示例