Page 1 of 1
Further DOM iteration
Posted: Wed Jan 10, 2018 11:48 am
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
Re: Further DOM iteration
Posted: Wed Jan 10, 2018 1:42 pm
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.
Re: Further DOM iteration
Posted: Tue Jan 23, 2018 10:32 pm
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
would retrieve the '299' but this is not happening.
Where am I wrong?
Many thanks
Alberto
Re: Further DOM iteration
Posted: Wed Jan 24, 2018 7:35 am
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.
Re: Further DOM iteration
Posted: Wed Jan 24, 2018 11:46 pm
by PioPio
Thank you very much, Salvador ! Everything works now.
Alberto
Re: Further DOM iteration
Posted: Mon Jul 15, 2019 5:43 am
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.
Re: Further DOM iteration
Posted: Mon Jul 15, 2019 8:43 am
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.
Re: Further DOM iteration
Posted: Mon Jul 15, 2019 11:56 am
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!