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 read a field value from a page?

Post Reply
egold2005
Posts: 11
Joined: Mon Aug 24, 2020 9:32 am

How to read a field value from a page?

Post by egold2005 »

Hello,
I used the following code with TWebBrowser to read a page value

function GetElementIdValue(AWebBrowser: TWebBrowser; const ATagName, ATagId, ATagAttrib: string): string;
var vDocument: IHTMLDocument2;
vBody: IHTMLElement2;
vTags: IHTMLElementCollection;
vTag: IHTMLElement;
vIdx: Integer;
begin
result := '';
if not Supports(AWebBrowser.Document, IHTMLDocument2, vDocument) then
exit; //raise Exception.Create('Invalid HTML document');

if not Supports(vDocument.body, IHTMLElement2, vBody) then
exit; //raise Exception.Create('Can''t find <body> element');

vTags := vBody.getElementsByTagName(UpperCase(ATagName));
for vIdx := 0 to Pred(vTags.length) do begin
vTag := vTags.item(vIdx, EmptyParam) as IHTMLElement;
if vTag.id = ATagId then
result := vTag.getAttribute(ATagAttrib, 0)
end
end;

What would be the similar code for Chromium browser?

Thanks a lot.
User avatar
salvadordf
Posts: 4575
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to read a field value from a page?

Post by salvadordf »

The DOMVisitor demo shows how to get and set values using :
  • JavaScript.
  • The CEF functions to visit the DOM.
  • The DevTools functions.
The recomemded way is using JavaScript and then send the results back to Delphi using the "console trick", which is also explained in that demo.
egold2005
Posts: 11
Joined: Mon Aug 24, 2020 9:32 am

Re: How to read a field value from a page?

Post by egold2005 »

Thanks Salvador, i got JavaScript method working in my own test application, however this method is async.

Should i use CEF functions for sync mode?
User avatar
salvadordf
Posts: 4575
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: How to read a field value from a page?

Post by salvadordf »

Most of the Chromium features are async, especially if you have to deal with the DOM because it "lives" in a different process.

I would recomend to modify the existing code in your application so it can use async functions.
Post Reply