我在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>
选项数量和值动态变化,
我怎样才能将选项放入数组中,所以我有..
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");
如果有人有任何代码要分享那就太棒了!
非常感谢斯图
答案 0 :(得分:2)
更新:在2017年以上,TEmbeddedWb不是一个很好的选择。请查看Delphi中的DCEF(铬浏览器)。
我知道如何使用TEmbeddedWB,最初来自现已解散的网站www.bsalsa.com,仍然可以在sourceforge和github使用,这是一个性能更高,功能更强大的功能替换TWebBrowser的IE包装器你可以使用这样的东西:
procedure Dummy;
var
element: IHTMLElement;
begin
element := EmbeddedWB1.GetActiveElement;
end;
获得元素之后,从IHTMLElement获取HTML是微不足道的。
我从我的应用程序中取出了所有的TWebBrowser并将TEmbeddedWB放入了十几个很好的错误修复程序,并且这样的功能,例如在这种情况下,它只是让得到活动控件(比如这个html SELECT(下拉列表))控制)很容易。
答案 1 :(得分:2)
使用名为TWebBrowser
的{{1}},您可以通过这种方式获取您的ID和文字:
Wb
修改:也添加了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;
。
Edit2:这是网页'select.htm':
option_texts