Question

I have the following Delphi code to fill form on a TWebBrowser:

procedure SetFieldValue(theForm: IHTMLFormElement; const fieldName: string; const newValue: string);
   var
     field: IHTMLElement;
     inputField: IHTMLInputElement;
     selectField: IHTMLSelectElement;
    textField: IHTMLTextAreaElement;
begin
  field := theForm.Item(fieldName, '') as IHTMLElement;
  if Assigned(field) then
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      // Make the change below to catch checks and radios.
      if (inputField.type_ = 'checkbox') or (inputField.type_ = 'radio') then
      begin
        if newValue = 'Y' then
          inputField.checked := True
        else
          inputField.checked := False;
      end
      else
        inputField.value := newValue;
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.value := newValue;
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      textField.value := newValue;
    end;
  end;
end;

HTML source looks something like this example:

<select name="xosid">
   <option value="" selected>-- choose one --</option>
   <option value="first">This is one</option>
   <option value="second">Another</option>
</select>

I would like to modify the above function to be able to select from a dropdown the 'Another' without knowing the value...

Any suggestions?

Thanks in advance,

Zsolt

Was it helpful?

Solution

to be able to select from a dropdown the 'Another' without knowing the value

So then by index ? option number 2 (starting with zero) ?

Let's start with searching for data type in Google

The 1st link is MSDN specifications for that datatype in Microsoft Internet Explorer. To the right you can see selectedIndex: Integer property.

So the code would be like

else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.selectedIndex := 2;
    end

And going back to the Google, the 3rd link to me was the ready source code example using that property.

OTHER TIPS

    selectField.value := (selectField.item(selectField.length-1,EmptyParam) as IHTMLOptionElement).value;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top