简体   繁体   中英

Delphi - How can I get a select from a twebbrowser into an array?

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

if anyone has any code to share that would be great!

many thanks

Stu

Update: In year 2017+, TEmbeddedWb is not such a great choice. Check out the DCEF (chromium browser) in Delphi instead.

I know how to do that using TEmbeddedWB, originally from the now defunct site www.bsalsa.com, still available at sourceforge and github , which is a higher-performance and more feature-filled IE wrapper that replaces TWebBrowser You use something like this:

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

Once you have the element, it's trivial to get its HTML from IHTMLElement.

I took all the TWebBrowser's out of my apps and put in TEmbeddedWB for a dozen great bug fixes, and features like this, such as in this case, it just makes getting active controls (like this html SELECT (drop down list) control) easy.

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;

Edit: added option_texts as well.

Edit2: 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM