德尔福 - 我怎样才能从一个twebbrowser选择到一个数组? [英] Delphi - How can I get a select from a twebbrowser into an array?
本文介绍了德尔福 - 我怎样才能从一个twebbrowser选择到一个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下选择在我的twebbrowser
I have the following select in my twebbrowser
<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>
选项中的值的数目和动态地改变,
the number of options and the values change dynamically,
我如何能得到的选项到一个数组,所以我必须..
how can i get the options into an array so I have..
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");
如果任何人有任何code共享,这将是伟大的!
if anyone has any code to share that would be great!
非常感谢
斯图
推荐答案
使用 TWebBrowser
名为 Wb的
即可让你的ID和文字是这样的:
Using a TWebBrowser
named Wb
you can get your ids and texts this way:
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:这是网页select.htm
This is the web page '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>
这篇关于德尔福 - 我怎样才能从一个twebbrowser选择到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文