Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

How to find with GetElementById

X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

How to find with GetElementById

Post 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
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to find with GetElementById

Post 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>
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: How to find with GetElementById

Post 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;
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to find with GetElementById

Post 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.
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: How to find with GetElementById

Post 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="
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: How to find with GetElementById

Post 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"
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to find with GetElementById

Post 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.
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: How to find with GetElementById

Post by X11 »

I forgot about recursion
X11
Posts: 49
Joined: Thu Jul 25, 2019 10:15 am

Re: How to find with GetElementById

Post by X11 »

Is your "SimpleDOMIteration()" example recursively traversing the DOM tree? Or not?
User avatar
salvadordf
Posts: 4565
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to find with GetElementById

Post 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.
Post Reply