delphi - How to select item by the "value" attribute in drop-down list? -
delphi - How to select <option> item by the "value" attribute in <select> drop-down list? -
in delphi application i'm using twebbrowser
control, have loaded html document, containing <select>
element (drop downwards list) few <option>
items (drop downwards list items). let's say, have next html document loaded in web browser:
<html> <body> <select id="combobox"> <option value="firstvalue">first value</option> <option value="secondvalue">second value</option> <option value="thirdvalue">third value</option> </select> </body> </html>
how can programatically select e.g. <option>
, value
attribute thirdvalue
? or in other words, how can programatically select 3rd item in drop downwards list, when know item's value
attribute thirdvalue
?
you can utilize ihtmlselectelement
interface selectedindex
property instance. showcase i've made next function.
the next function tries find, , select (if found) <option>
(a drop downwards list item) of given value
attribute value in specified <select>
element (drop downwards list). if no <option>
found, current drop downwards list selection cleared (no item selected).
parameters:
adocument - interface input html document aelementid - id of<select>
element (element id of drop downwards list) aoptionvalue - searched <option>
element value (value of drop downwards list item) return value:
if <option>
given value
found (and selected), homecoming value index of alternative in specified drop downwards list, -1 otherwise.
source code:
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; := 0 ahtmlelement.length - 1 if (ahtmlelement.item(i, i) ihtmloptionelement).value = avalue begin result := i; break; end; end; begin result := -1; if supports(adocument, iid_ihtmldocument3, htmldocument) begin if supports(htmldocument.getelementbyid(aelementid), iid_ihtmlselectelement, htmlelement) begin result := indexofvalue(htmlelement, aoptionvalue); htmlelement.selectedindex := result; end; end; end;
example usage:
to select item thirdvalue
value in drop downwards list html document question it's possible utilize code (assuming in webbrowser1
component here loaded document):
procedure tform1.button1click(sender: tobject); var index: integer; begin index := selectoptionbyvalue(webbrowser1.document, 'combobox', 'thirdvalue'); if index <> -1 showmessage('option found , selected on index: ' + inttostr(index)) else showmessage('option not found or function failed (probably due ' + 'invalid input document)!'); end;
example html document question:
<html> <body> <select id="combobox"> <option value="firstvalue">first value</option> <option value="secondvalue">second value</option> <option value="thirdvalue">third value</option> </select> </body> </html>
delphi dom twebbrowser
Comments
Post a Comment