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.

Further DOM iteration

Post Reply
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Further DOM iteration

Post by PioPio »

Hello,

I was looking again into the DOMVisitor example and I can see the procedure SimpleDOMIteration iterates the several nodes of the DOM.
However, inside each ICefDomNode there might be multiple sub elements (tag couples, text...). I was wondering if there is a way in CEF4Delphi to iterate each of these elements as well.

Many thanks
Alberto
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Further DOM iteration

Post by salvadordf »

Hi,

CEF3 allows you to access a generic interface for nodes (ICefDomNode) and an interface for the document (ICefDomDocument)

Basically, you can iterate the nodes the same way you would search in a TTreeView : FirstChild, NextSibling, LastChild, Parent, etc.
To read and write the attributes you can use : GetElementAttributes, GetElementAttribute, SetElementAttribute, HasElementAttribute, etc.

ICefDomNode is described here :
http://magpcss.org/ceforum/apidocs3/pro ... MNode.html

ICefDomDocument is described here :
http://magpcss.org/ceforum/apidocs3/pro ... ument.html

This is the CEF source code with the comments for both of them :
https://bitbucket.org/chromiumembedded/ ... ew-default

If you need anything else use JavaScript to search the DOM.
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Re: Further DOM iteration

Post by PioPio »

Hello again,

I have completed the iteration successfully and I have the node in a ICefDomNode variable. I want to extract the information from that now.
The following content of the AsMarkup property of the variable
<span class="winner ">'#$A' 299'#$A' </span>
and I want to extract the value, 299 in my case.

I was under the impression that either

Code: Select all

TempNode.GetElementAttribute('value');
or

Code: Select all

TempNode.GetValue;
would retrieve the '299' but this is not happening.

Where am I wrong?

Many thanks
Alberto
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Further DOM iteration

Post by salvadordf »

Hi,

If you have found the <span> node then you can get the inner text by visiting its child nodes and checking the IsText property, or reading the ElementInnerText property on the <span> element but this will give you unparsed text.
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

Re: Further DOM iteration

Post by PioPio »

Thank you very much, Salvador ! Everything works now.

Alberto
User avatar
Paulo França
Posts: 17
Joined: Mon Jul 15, 2019 2:22 am
Location: Brazil
Contact:

Re: Further DOM iteration

Post by Paulo França »

salvadordf wrote: Wed Jan 10, 2018 1:42 pm ...If you need anything else use JavaScript to search the DOM.
Using Delphi? How?
That would be very handy, because I need to trigger some input element events and I can't see how to accomplish this via usual properties/methods.
Thanks in advance.
Last edited by Paulo França on Mon Jul 15, 2019 11:57 am, edited 1 time in total.
User avatar
salvadordf
Posts: 4052
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Further DOM iteration

Post by salvadordf »

Paulo França wrote: Mon Jul 15, 2019 5:43 am
salvadordf wrote: Wed Jan 10, 2018 1:42 pm ...If you need anything else use JavaScript to search the DOM.
Using Delphi? How?
That would be very handy, because I need to trigger some input element events and I can't see how to accomplish this via usual properties/methods.
Thanks in advance.
Hi,

You can execute custom JavaScript code with the TChromium.ExecuteJavaScript procedure.

These are some of the JavaScript functions to navigate the DOM :
https://www.w3schools.com/js/js_htmldom_navigation.asp

As soon as you find what you need in the DOM you can call a "JavaScript extension". You can register an extension to create custom JS functions that execute Delphi code directly from JavaScript as you can see in the JSExtension and JSRTTIExtension demos.

Those demos register "myextension" which is defined in uTestExtension.pas (TTestExtension) and they use ExecuteJavaScript to set events, get the HTML text and even set a mutation observer in the search box at google.com

Chromium uses a different process for JavaScript and you will need to send the results from the extension code to the main browser process with a process message.

Read all the code comments in the JSExtension and JSRTTIExtension demos for more details.
User avatar
Paulo França
Posts: 17
Joined: Mon Jul 15, 2019 2:22 am
Location: Brazil
Contact:

Re: Further DOM iteration

Post by Paulo França »

Thanks for the detailed answer.
I've just implemented what I needed (for now) via ExecuteJavaScript(). It worked at a glance!
Thank you!
Post Reply