i am trying to build myself the XPath for an element clicked (e.g.) on a website inside of a TWebBrowser. if you know some available functions or an other way please give me a hint.
procedure TForm1.Button2Click(Sender: TObject);
var
Elem: IHTMLElement;
ss: string;
ii: Integer;
function GetElemOrder(e: IHTMLElement): integer;
var
jj: Integer;
ee: IHTMLElement;
Coll: IHTMLElementCollection;
begin
//get all children of the elem parent - the elem is a child itself
Coll := e.parentElement.children as IHTMLElementCollection;
//filter the tags to minimize the list (only the same tags as the elem)
Coll := Coll.tags(e.tagName) as IHTMLElementCollection;
for jj := 0 to Coll.length - 1 do begin
inc(Result);
ee := Coll.item(jj, EmptyParam) as IHTMLElement;
//go through the list and check if the elem was found
if (e = ee) then // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< this doesn't work
//return the actual count
Exit;
end;
Result := 0; // result 0 means always an error, as the elem wasn't found in the collection
end;
begin
//get element obj
Elem := GetElementById(wb.Document, 'input_username') as IHTMLElement;
if Assigned(Elem) then
//build the xpath until reach the root
while elem.parentElement <> nil do begin
// get the order of the elem in the list of children e.g. 3rd DIV from 5 DIVs
ii := GetElemOrder(Elem);
ss := '/' + Elem.tagName + '[' + ii.ToString + ']' + ss;
//go to the next level
Elem := Elem.parentElement;
end;
end;
it works so far but i have a problem to compare 2 IHTMLElements. What am I doing wrong?