Assign value to a Select by the text of the option via Webbrowser

Asked

Viewed 463 times

2

I need to set the value of a field Select of a page that is loaded into the Twebbrowser component in Delphi 7. This procedure I can do in other fields where values are equal to the options texts, but in another field as are hundreds of values and the text of the option Select is different from the value, so I can’t. The Procedure I need 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;

Procedure above only serves when the text of the field option Select is equal to the option value. For example:

SelectOptionByValue(web.Document, 'fNaturalidade', DMCadastros.cdsClientesUF.AsString);

1 answer

1


I just found out, a little change in the function line to:

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

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

Even so thank you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.