2

私は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");

誰かが共有するコードを持っているなら、それは素晴らしいことです!

どうもありがとう

ストゥ

4

2 に答える 2

2

更新: 2017 年以降、TEmbeddedWb はそれほど優れた選択肢ではありません。代わりに、Delphi の DCEF (クロム ブラウザ) を確認してください。

TEmbeddedWB を使用してそれを行う方法を知っています。もともとは現在は機能していないサイト www.bsalsa.com からのもので、まだsourceforgegithubで入手できます。これは、TWebBrowser を置き換える、より高性能で機能が豊富な IE ラッパーです。次のようなものを使用します。

 procedure Dummy;
 var
    element: IHTMLElement;
 begin
    element := EmbeddedWB1.GetActiveElement;
 end;

要素を取得したら、IHTMLElement からその HTML を取得するのは簡単です。

私は自分のアプリからすべての TWebBrowser を取り除き、TEmbeddedWB を入れて多数のバグ修正を行いました。このような機能は、この場合のように、アクティブなコントロール (この html SELECT (ドロップダウン リスト) コントロールのような) を簡単に取得できるようにするだけです。 .

于 2011-07-21T03:07:34.677 に答える
2

TWebBrowser名前付きを使用するとWb、次の方法で 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>
于 2011-07-21T15:06:31.877 に答える