-1

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

modzsi
  • 185
  • 2
  • 12
  • You mean that you want to select the ` – TLama Jul 19 '13 at 13:25
  • 1
    Here you have [`select – TLama Jul 19 '13 at 13:37
  • I would like to select – modzsi Jul 19 '13 at 13:38
  • Then you might be looking for a [`code like this`](http://pastebin.com/SUs8thmy). I've improved that quite unsafe part with `tagName` properties. – TLama Jul 19 '13 at 15:51
  • Thanks TLama, Just needed this again, and found your code tiptop :) – modzsi Aug 19 '14 at 23:21

2 Answers2

0

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.

Arioch 'The
  • 15,799
  • 35
  • 62
  • Do not rely on `tagName`, rather use e.g. `var SelectElement: IHTMLSelectElement; begin ... if Supports(field, IID_IHTMLSelectElement, SelectElement) then...`. – TLama Jul 19 '13 at 13:33
  • @Tlama - this is to topicstarter rather than me – Arioch 'The Jul 19 '13 at 13:44
0
    selectField.value := (selectField.item(selectField.length-1,EmptyParam) as IHTMLOptionElement).value;