delphi中Ihtmldocument2介面的使用

來源:互聯網
上載者:User

MSHTML是微軟公司的一個COM組件,該組件封裝了HTML語言中的所有元素及其屬性,通過其提供的標準介面,可以訪問指定網頁的所有元素.

 

  MSHTML物件模型是由一些對象和集合組成的.處於根部的是HTML,描述了開啟頁面的1個視窗,包括一系列集合和對象。如Frames集合,History,Location,Navigator,Document,Vi—sum,Event對象等.其中描述呈現在客戶視窗實際網頁的是Document對象。由一系列的屬性、方法、對象和集合組成.其中All集合中包含網頁中所有標記(Tag)元素,其主要的方法和屬性有:

 

  (1)Length(長度):即標記出現的個數,可以把標記的集合理解為從0開始的一維數組,其次序按照標記在網頁位置排列;

 

  (2)Tags(標記):用於過濾出給定標記的集合,如Doc.Al1.Tags(P)得到所有分區段標記P;

 

  (3)Item(項目):用於選擇集合中的某1個元素,如object.item(0)得到集合的第1個元素,而object.item(i)得到第i+1個元素.

 

  此外,IHTMLElement也是個常用的集合對象,代表網頁中指定標記的集合,通過這個集合對象,可以得到網頁上特定標記的內容.IHTMLElement有4個主要屬性:

 

  (1)InnerText:開始標記和結束標記之間的文本;

 

  (2)InnerHTML:開始標記和結束標記之間的文本和HTML;

 

  (3)OuterText:對象的文本;

 

  (4)OuterHTML:對象的文本和HTML.

 

注意:使用前加入單元mshtml

 

示範表單提交

 

procedure TForm1.Button1Click(Sender: TObject);
var
Doc:IHTMLDocument2;
input:OleVariant;
userinputelement,pwdinputelement:ihtmlinputelement;
begin
doc:=webbrowser1.document as ihtmldocument2;
userinputelement:=(doc.all.item('user'(也就是網頁中使用者名稱控制項的名字),0) as ihtmlinputelement);
userinputelement.value:=edit1.text;(也就是你要向網頁輸入的東西)
pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement);
pwdinputelement.value:=edit2.text;
input:=doc.all.item('submit',0);
input.click;
end;

 

當提交資料按鈕沒有NAME屬性時,採用如下方法:

 

procedure TForm1.Button1Click(Sender: TObject);
var
Doc:IHTMLDocument2;
form:ithmlformelement;
userinputelement,pwdinputelement:ihtmlinputelement;
begin
doc:=webbrowser1.document as ihtmldocument2;
userinputelement:=(doc.all.item('user'(也就是網頁中使用者名稱控制項的名字),0) as ihtmlinputelement);
userinputelement.value:=edit1.text;(也就是你要向網頁輸入的東西)
pwdinputelement:=(doc.all.item('password',0) as ihtmlinputelement);
pwdinputelement:=edit2.text;
form:=(doc.all.item('login_form',0) as ihtmlformelement):
form.submit;
end;

 

登入"按鈕一般都是網頁中預設的斷行符號按鈕,所以可以用上面代碼來代替前面的點擊按鈕

 

讀取某網頁內容

在建立表單的時候開啟一網頁

 

 

webbrowser1.navigate('http://www.baidu.com')

 

procedure TForm1.Button1Click(Sender: TObject);

 

begin

 

memo1.lines.add(ihtmldocument2(webbrowser1.document).body.outerhtml);

 

end;

 

 

這樣一來memo1中顯示www.baidu.com的html

 

利用webbrowser1控制項瀏覽某txt中檔案的內容,並以html形式來顯示出來

 

在webbrowser1的控制項中的ondocumentcomplete事件加入如下代碼

 

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;

 

const pDisp: IDispatch; var URL: OleVariant);

 

var

 

webdoc:htmldocument ;

 

webbody:htmlbody;

 

begin

 

webdoc:=webbrowser1.document as htmldocument;

 

webbody:=webdoc.body as htmlbody;

 

webbody.insertAdjacentHTML('beforeend','<form method="POST" action="">');

 

webbody.insertAdjacentHTML('beforeend','Password: ');

 

webbody.insertAdjacentHTML('beforeend','<input type="password" >');

 

webbody.insertAdjacentHTML('beforeend','<input type="submit" value="LOGIN" >');

 

webbody.insertAdjacentHTML('beforeend',' ');

 

webbody.insertAdjacentHTML('beforeend','</form>');

 

end;

 

 

讀取的時候,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.