Assign value to a Select by option text via Webbrowser

2

I need to set the value of a Select field of a page that is loaded inside the TWebBrowser component in Delphi 7. This procedure I can do in other fields where the values are the same as the text of the options, but in another field as there are hundreds of values and the Select option text is different from the value, so I can not. The procedure I'm needing is like this below:

function SelectOptionByValue(const ADocument: IDispatch; const AElementID,
  AOptionValue: WideString): Integer;
var
  HTMLDocument: IHTMLDocument3;
  HTMLElement: IHTMLSelectElement;

  function IndexOfValue(const AHTMLElement: IHTMLSelectElement;
    const AValue: WideString): Integer;
  var
    I: Integer;
  begin
    Result := -1;
    for I := 0 to AHTMLElement.length - 1 do
      if (AHTMLElement.item(I, I) as IHTMLOptionElement).value = AValue then
      begin
        Result := I;
        Break;
      end;
  end;

begin
  Result := -1;
  if Supports(ADocument, IID_IHTMLDocument3, HTMLDocument) then
  begin
    if Supports(HTMLDocument.getElementById(AElementID), IID_IHTMLSelectElement,
      HTMLElement) then
    begin
      Result := IndexOfValue(HTMLElement, AOptionValue);
      HTMLElement.selectedIndex := Result;
    end;
  end;
end;

The above procedure only works when the option text of the Select field is equal to the value of the option. For example:

SelectOptionByValue(web.Document, 'fNaturalidade', DMCadastros.cdsClientesUF.AsString);
    
asked by anonymous 15.07.2015 / 02:01

1 answer

1

I just discovered a small change in the function line to:

if (AHTMLElement.item(I, I) as IHTMLOptionElement).text = AValue then

I just changed the "value" property to "text"

Thank you all the same!

    
15.07.2015 / 02:31