日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Delphi自动提交网页表单和获取框架网页源码

發布時間:2025/3/15 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delphi自动提交网页表单和获取框架网页源码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這兩個問題的實現原理其實是差不多的,所以放在一起介紹,單元MSHtml封裝了我們需要的功能。

首先,新建一個DELPHI工程,在USES部分添加MSHtml單元的引用。

然后,在窗體上放置一個TWebBrowser控件和四個按鈕。

最后,編寫四個按鈕的響應代碼:

1. 自動提交網頁表單

procedure?TForm1.Button1Click(Sender: TObject);
begin
? WebBrowser1.Navigate('http://www.baidu.com');
end;

procedure?TForm1.Button2Click(Sender: TObject);
var
? doc: IHTMLDocument2;
? oleObj: OleVariant;
begin
? doc := WebBrowser1.Document?as?IHTMLDocument2;
??if?doc =?nil then?Exit;
? oleObj := doc.all.item('wd', 0)?as?IHTMLElement2;
??//網頁有一個名為“wd”的文本框:<input type="text" name="wd" id="kw" maxlength="100">
? oleObj.Value := 'Delphi';?//為文本框賦值
? oleObj := doc.all.item('su', 0)?as?IHTMLElement2;
??//網頁有一個ID為“su”的按鈕:<input type="submit" value="百度一下" id="su">
? oleObj.Click;??//點擊按鈕,提交表單
end;?

2. 獲取框架網頁源碼

?procedure?TForm1.Button3Click(Sender: TObject);
begin
? WebBrowser1.Navigate('http://含有框架的網頁URL');
end;

procedure?TForm1.Button4Click(Sender: TObject);
var
? doc, framedoc: IHTMLDocument2;
? frame_dispatch: IDispatch;
? ole_index: OleVariant;
? i: Integer;
begin
? doc := WebBrowser1.Document?as?IHTMLDocument2;
??if?doc =?nil then?Exit;
??for?i := 0?to?doc.frames.length - 1?do
??begin
??? ole_index := i;
??? frame_dispatch := doc.frames.item(ole_index);
????if?frame_dispatch =?nil then?Continue;
??? framedoc := (frame_dispatch?as?IHTMLWindow2).document;
????if?framedoc =?nil then?Continue;
??? ShowMessage(framedoc.body.innerHTML);
??end;
end;
?

3. 獲取網頁所有鏈接

procedure TForm1.Button1Click(Sender: TObject);
var
? elem: IHTMLElement;
? coll: IHTMLElementCollection;
? i: integer;
? url, title: string;
begin
? coll := (WebBrowser1.Document as IHTMLDocument2).all;
? coll := (coll.tags('a') as IHTMLElementCollection);
? for i := 0 to coll.Length - 1 do
???? begin?//?? 循環取出每個鏈接
????? elem := (coll.item(i, 0) as IHTMLElement);
????? url := Trim(string(elem.getAttribute(WideString('href'), 0)));
????? title := elem.innerText;
????? ShowMessage(Format('鏈接標題:%s,鏈接網址:%s', [title, url]));
???? end;
end;

總結

以上是生活随笔為你收集整理的Delphi自动提交网页表单和获取框架网页源码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。