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
Further DOM iteration
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
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
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Further DOM iteration
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.
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
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
I was under the impression that either or would retrieve the '299' but this is not happening.
Where am I wrong?
Many thanks
Alberto
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
and I want to extract the value, 299 in my case.<span class="winner ">'#$A' 299'#$A' </span>
I was under the impression that either
Code: Select all
TempNode.GetElementAttribute('value');
Code: Select all
TempNode.GetValue;
Where am I wrong?
Many thanks
Alberto
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Further DOM iteration
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.
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
Thank you very much, Salvador ! Everything works now.
Alberto
Alberto
- Paulo França
- Posts: 17
- Joined: Mon Jul 15, 2019 2:22 am
- Location: Brazil
- Contact:
Re: Further DOM iteration
Using Delphi? How?salvadordf wrote: Wed Jan 10, 2018 1:42 pm ...If you need anything else use JavaScript to search the DOM.
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.
- salvadordf
- Posts: 4565
- Joined: Thu Feb 02, 2017 12:24 pm
- Location: Spain
- Contact:
Re: Further DOM iteration
Hi,Paulo França wrote: Mon Jul 15, 2019 5:43 amUsing Delphi? How?salvadordf wrote: Wed Jan 10, 2018 1:42 pm ...If you need anything else use JavaScript to search the DOM.
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.
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.
- Paulo França
- Posts: 17
- Joined: Mon Jul 15, 2019 2:22 am
- Location: Brazil
- Contact:
Re: Further DOM iteration
Thanks for the detailed answer.
I've just implemented what I needed (for now) via ExecuteJavaScript(). It worked at a glance!
Thank you!
I've just implemented what I needed (for now) via ExecuteJavaScript(). It worked at a glance!
Thank you!