[英]Delphi - How can I get a select from a twebbrowser into an array?
我的 twebbrowser 中有以下 select
<Select name="ship_to_method">
<option value="1941">Royal Mail Standard Delivery at £1.45 </option>
<option value="1942">Courier Standard Delivery at £4.64 </option>
<option value="1943">Royal Mail Priority Delivery at £1.66 </option>
<option value="1944">Courier Priority Delivery at £5.15 </option>
</select>
選項的數量和值動態變化,
我怎樣才能將選項放入數組中,所以我有..
option_ids=(1941,1942,1943,1944);
option_texts=("Royal Mail Standard Delivery at £1.45","Courier Standard Delivery at £4.64","Royal Mail Priority Delivery at £1.66","Courier Priority Delivery at £5.15");
如果有人有任何代碼可以分享,那就太好了!
非常感謝
斯圖
更新:在 2017+ 年,TEmbeddedWb 並不是一個很好的選擇。 請查看 Delphi 中的 DCEF(鉻瀏覽器)。
我知道如何使用 TEmbeddedWB 來做到這一點,最初來自現已解散的網站 www.bsalsa.com,仍然可以在sourceforge和github 獲得,這是一個更高性能和更多功能的 IE 包裝器,它取代了 TWebBrowser:
procedure Dummy;
var
element: IHTMLElement;
begin
element := EmbeddedWB1.GetActiveElement;
end;
一旦你有了元素,從 IHTMLElement 獲取它的 HTML 就很簡單了。
我從我的應用程序中取出所有 TWebBrowser 並放入 TEmbeddedWB 以進行十幾個出色的錯誤修復和類似這樣的功能,例如在這種情況下,它只是使獲得活動控件(如 html SELECT(下拉列表)控件)變得容易.
使用名為Wb
的TWebBrowser
,您可以通過以下方式獲取您的 id 和文本:
uses MSHTML;
var
Disp: IDispatch;
SelEl: IHTMLSelectElement;
i: Integer;
OptionEl: IHTMLOptionElement;
option_ids: array of WideString;
option_texts: array of WideString;
begin
// load test web page containing your SELECT
Wb.Navigate('c:\temp\select.htm');
// wait for browser to finish loading
while Wb.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;
// search the document for the SELECT element with the given name
Disp:=(Wb.ControlInterface.Document as IHTMLDocument2).all.item('ship_to_method', EmptyParam);
// EDIT: the following two lines are demonstrating how to get the element with focus
// simulate user selection by setting focus to SELECT element
(Disp as IHTMLElement2).focus;
// now ask document for active element which should be the SELECT element
Disp:=(Wb.ControlInterface.Document as IHTMLDocument2).activeElement;
// basic error checking and acquiring of IHTMLSelectElement interface which is needed to access single OPTIONs within the SELECT
if Assigned(Disp) and Supports(Disp, IHTMLSelectElement, SelEl) then
begin
// prepare array
SetLength(option_ids, SelEl.length);
SetLength(option_texts, SelEl.length);
// get OPTIONs from SELECT
for i:=0 to SelEl.length-1 do
begin
OptionEl := SelEl.Item(i,EmptyParam) as IHTMLOptionElement;
// voila - read value and text of option element, store in arrays
option_ids[i] := OptionEl.Value;
option_texts[i] := OptionEl.Text;
end;
end;
// option_ids now contains your IDs
// option_texts now contains your texts
end;
編輯:也添加了option_texts
。
Edit2:這是 web 頁面“select.htm”:
<html>
<head>
</head>
<body>
<Select name="ship_to_method">
<option value="1941">Royal Mail Standard Delivery at £1.45 </option>
<option value="1942">Courier Standard Delivery at £4.64 </option>
<option value="1943">Royal Mail Priority Delivery at £1.66 </option>
<option value="1944">Courier Priority Delivery at £5.15 </option>
</select>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.