arrays - Delphi - 如何将 twebbrowser 中的选择放入数组中?

标签 arrays delphi select twebbrowser

我在我的 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");

如果有人有任何代码可以分享,那就太好了!

非常感谢

斯图

最佳答案

使用名为 WbTWebBrowser,您可以通过以下方式获取您的 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

编辑2:这是网页“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>

关于arrays - Delphi - 如何将 twebbrowser 中的选择放入数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6767229/

相关文章:

PHP-从选择框中获取没有值的数据

arrays - SWIFT:将 CGPoint 与数组中的图像关联

delphi - 如何打开和保存大量子表单以对父表单更改使用react?

delphi - 比较 JPEG 图像最快的解决方案是什么? (忽略元数据,仅 "pixels")

c# - Delphi 到 .NET + C#

无需选择即可 hibernate 、插入或更新

Mysql:选择任意年份 1 月 22 日至 2 月 3 日之间的时间戳

php - 取消设置数组中的索引将它变成一个对象

c++ - 将数组 int 值转换为 double 值

javascript - Java String 声明字符串数组返回未定义