Delphi: How to check if element obtained by getElementById really exists?

1

I have the following code:

procedure TMainForm.ValidarAcesso;
var
  doc: variant;
  element: variant;
begin
  doc := coHTMLDocument.Create as IHTMLDocument2;
  doc.write(memHtml.Text);
  try
    element := doc.getElementById('theElementId');
    if VarToStr(element) <> '' then
      ShowMessage(element.innerText)
    else
      ShowMessage('Acesso realizado com sucesso!');
  except
  end;
end;

I create a COM HTML Document and load the Html source of a page in it and then search for an html element using getElementById. This Works!

But how do I add a control to see if the element really exists / was found on the / html page?

When the element is not found, by hovering over the element variable, Delphi displays the value $00000000 .

I've already tested:

VarToStr(element) <> ''
element <> $00000000
VarType(element) ...
element <> null
element <> nil

But none of them worked!

    
asked by anonymous 09.08.2014 / 04:42

1 answer

1

Example found on the web and it worked:

if Assigned(TVarData(element).VPointer) then
  ShowMessage(element.innerText)
else
  ShowMessage('Acesso realizado com sucesso!');
    
09.08.2014 / 14:33