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

Asked

Viewed 1,075 times

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 a page’s HTML source into it and then search for an html element using getElementById.
It Works!

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

When the element is not found, when 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!

  • In javascript (in the case of Webview) a null variable is undefined, try this or the null of Delphi, I think it’s Nil right? (Note: I know practically nothing of Delphi. I’m just trying to help)

  • Okay. It’s just that since it was a new tag I believed you were wrong, because I know every language, the Web component is called Webview. Good to know you solved your problem.

  • You have tested the method assigned?

1 answer

1

Example found on the web and that worked:

if Assigned(TVarData(element).VPointer) then
  ShowMessage(element.innerText)
else
  ShowMessage('Acesso realizado com sucesso!');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.