Page 1 of 2

How to find with GetElementById

Posted: Thu Aug 29, 2019 11:10 am
by X11
Image

Tell me please, how to find tel nubmer?
What to specify in parameter "NodeID"?

Code: Select all

procedure SimpleNodeSearch(const aDocument: ICefDomDocument; const frame: ICefFrame; const NodeID: string);
var
  TempNode: ICefDomNode;
  msg: ICefProcessMessage;
begin
  try
    if aDocument <> nil then
    begin
      TempNode := aDocument.GetElementById(NodeID);
      msg := TCefProcessMessageRef.New(msg_for_chromium);

      if TempNode <> nil then
      begin
        msg.ArgumentList.SetString(0, 'Node found: ' + NodeID);
        msg.ArgumentList.SetString(1, 'Node name: ' + TempNode.Name);
        msg.ArgumentList.SetString(2, 'ElementInnerText: ' + TempNode.ElementInnerText);
        msg.ArgumentList.SetString(3, 'ElementTagName: ' + TempNode.ElementTagName);

      end
      else
        msg.ArgumentList.SetString(0, 'ERROR. Node not found: ' + NodeID);

      frame.SendProcessMessage(PID_BROWSER, msg);
    end;
  except
    on e : exception do
      if CustomExceptionHandler('SimpleNodeSearch', e) then raise;
  end;
end;
for example
page url doska.plus/chita/search/nedvizhimost/prodazha-nedvizhimosti/prodazha-kvartir/prodam-3-komn-kvartiru-49440.html
thanx

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:03 pm
by salvadordf
You can only use GetElementById to search HTML elements that have an "id" attribute.

In this example you can call aDocument.GetElementById('myHeader') to get the H1 element :

Code: Select all

<html><body><h1 id="myHeader">Hello World!</h1></body></html>

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:16 pm
by X11
OK, then maybe during an iteration over the DOM?

Code: Select all

procedure SimpleDOMIteration(const aDocument: ICefDomDocument; const frame: ICefFrame);
var
  TempHead, TempChild : ICefDomNode;
  msg: ICefProcessMessage;
  attrList: TStrings;
begin
  try
    if aDocument <> nil then
    begin
        TempHead := aDocument.Body;

        if TempHead <> nil then
        begin
          TempChild := TempHead.FirstChild;

          while TempChild <> nil do
          begin
            Msg := TCefProcessMessageRef.New(msg_for_chromium);
            if TempChild.ElementInnerText = '' then
            begin
              TempChild := TempChild.NextSibling;
              Continue;
            end;

            msg.ArgumentList.SetString(0, 'name: ' + Trim(TempChild.Name));
            msg.ArgumentList.SetString(1, 'TagName: ' + Trim(TempChild.ElementTagName));

            attrList := TStringList.Create;
            try
              TempChild.GetElementAttributes(attrList);

              msg.ArgumentList.SetString(2, 'attrList: ' + attrList.Text);
            finally
              attrList.DisposeOf;
            end;

            msg.ArgumentList.SetString(3, 'InnerText: ' + Trim(TempChild.ElementInnerText));
            msg.ArgumentList.SetString(4, '**********************');


            frame.SendProcessMessage(PID_BROWSER, msg);

            TempChild := TempChild.NextSibling;
          end;
        end;
    end;
  except
    on e : exception do
      if CustomExceptionHandler('SimpleDOMIteration', e) then raise;
  end;
end;

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:22 pm
by salvadordf
If that element doesn't have an ID attribute search with an iteration over the DOM using the functions ICEFDomNode.HasElementAttribute and ICEFDomNode.GetElementAttribute on each node to find what you are looking for.

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:29 pm
by X11
then I Copy InnerHTML in Mozilla Firefox, I see "<span><a href="tel:+xxxxxxx0">+xxxxxxx0</a></span>"

But

Code: Select all

TempChild.ElementInnerText
returns without tags "<span>" or "<a href="

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:41 pm
by X11
prn scr
http://prntscr.com/oz52ad
Image

Code: Select all

            if TempChild.HasElementAttribute('href') then
              msg.ArgumentList.SetString(4, 'href: ' + Trim(TempChild.GetElementAttribute('href')));
but code can`t found attribute "href"

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:48 pm
by salvadordf
Use ICEFDomNode.AsMarkup to get the contents of this node as markup.
Use ICEFDomNode.ElementInnerText to get the inner text of the element.

If TempChild really is the "A" element and not a parent or sibling node then TempChild.HasElementAttribute('href') should be TRUE, unless there's a bug in Chromium.

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 12:59 pm
by X11
I forgot about recursion

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 1:02 pm
by X11
Is your "SimpleDOMIteration()" example recursively traversing the DOM tree? Or not?

Re: How to find with GetElementById

Posted: Thu Aug 29, 2019 4:28 pm
by salvadordf
SimpleDOMIteration only iterates through the first generation child elements of the HEAD element and it doesn't goes any further. It's not recursive.