本文共 4546 字,大约阅读时间需要 15 分钟。
WebBrowser 操作记要 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); var s: string; i: Integer; begin s := WebBrowser1.OleObject.document.Script.str; i := WebBrowser1.OleObject.document.Script.num; ShowMessage(s); //Hello ShowMessage(IntToStr(i)); //99 //也可以这样读: s := WebBrowser1.OleObject.document.parentWindow.str; i := WebBrowser1.OleObject.document.parentWindow.num; ShowMessage(s); //Hello ShowMessage(IntToStr(i)); //99 end; 假如网页中有这样的语句: //调用网页脚本中的函数: procedure TForm1.Button1Click(Sender: TObject); begin WebBrowser1.OleObject.document.parentWindow.MB(); //HTML-Js //如需指定脚本语言, 需要: WebBrowser1.OleObject.document.parentWindow.execScript('MB()','JavaScript'); //HTML-Js end; 假如有这样的脚本: //判断网页及内部框架网页是否全部下载完毕 procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin if WebBrowser1.Application = pDisp then begin Text := '网页下载完毕!'; end; end; //改变背景色或背景图片: WebBrowser1.OleObject.document.body.bgcolor := '#FF0000'; WebBrowser1.OleObject.document.body.background := '...图片地址'; //操作有 ID 标签的对象: var s: string; begin s := 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> //获取网页源代码 var s: string; begin s := 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); begin with Msg do begin if 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; begin if IsChild(WebBrowser1.Handle, Msg.Hwnd) and ((Msg.Message = WM_RBUTTONDOWN) or (Msg.Message = WM_RBUTTONUP)) then begin GetCursorPos(mPoint); //得到光标位置 PopupMenu1.Popup(mPoint.X, mPoint.Y); //弹出popupmenu1的菜单 Handled:=True; end; end; //新页面写入 begin WebBrowser1.Navigate('about:blank'); WebBrowser1.OleObject.Document.Writeln('ok'); end; //从流中写入: var ms: TMemoryStream; begin ms := TMemoryStream.Create; Memo1.Lines.SaveToStream(ms); ms.Position := 0; (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)); ms.Free; end; //禁止提示脚步错误 procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); begin WebBrowser1.Silent := True; end; 以上转自万一的 Delphi 博客 //禁止弹出窗口 procedure TForm1.wb1NewWindow2(ASender: TObject; var ppDisp: IDispatch; var Cancel: WordBool); begin Cancel := True; Wb1.OleObject.document.parentWindow.execScript('window.alert=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.open=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.close=null;','JavaScript'); Wb1.OleObject.document.parentWindow.execScript('window.showModalDialog=null;','JavaScript'); Wb1.OleObject.document.parentwindow.execscript('function confirm() {return true;}','Javascript');//自动点击确认框 end; 获取网页所有框架的源代码: function GetSources(FWB:TWebbrowser):String; var i : Integer; title : string; s : string; ole_index : OleVariant; frame_dispatch : IDispatch; frame_win : IHTMLWindow2; doc2 : IHTMLDocument2; begin try Result := ''; s := title+(FWB.Document as Ihtmldocument2).body.outerHTML; doc2 := FWB.Document as IHTMLDocument2; if doc2.Frames.length>0 then begin for i :=0 to doc2.Frames.length-1 do begin ole_index := i; frame_dispatch := doc2.Frames.Item(ole_index); if Assigned(frame_dispatch) then begin frame_win := frame_dispatch as IHTMLWindow2; frame_doc := frame_win.document; if Assigned(frame_doc.body) then s := s+IHTMLDocument2(frame_doc).body.outerHTML; end; end; end; Result := s; except end; end; 注:需要引用mshtml,这里只涉及到含有一层框架,如果有多层框架自己去扩充即可;同理利用相同方法可以获取所有框架的超链接、图片等信息。
转载于:https://www.cnblogs.com/xtfnpgy/p/9285438.html