delphi webbrowser 經常用法示範範例

來源:互聯網
上載者:User

標籤:style   ar   io   color   os   sp   for   java   on   

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為button名稱    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);    //找到登入username的輸入框o.value := ‘TEST‘;o := WebBrowser.oleobject.document.all.item(‘LoginPassword‘,0); //找到登入password的輸入框o.value := ‘TEST‘WebBrowser.oleobject.document.Forms.Item(0, 0).submit;           //第一個表單提交{o :=WebBrowser.oleobject.document.all.item(‘Login‘,0);           //或者用指定表單名稱提交o.Click;   //點擊操作,對其他對象也可相同操作}end;多個frames的輸入,FrameIndex為Frame的序號varo : Olevariant;begin//找到登入username的輸入框o := WebBrowser.oleobject.document.documentelement.document.frames.item(FrameIndex).document.all.item(‘LoginUserID‘,0);o.value := ‘TEST‘;//找到登入password的輸入框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 經常用法示範範例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.