I am using TWebBrowser
in Delphi.
I need help to get an HTML element by its position.
The element I need is in a frame. Using elementFromPoint()
:
Document.elementFromPoint(X, Y)
I am getting the frame itself, but not what is inside of it.
I tried to get it using this:
WebBrowser.OleObject.Document.Frames
But this gives me an access error when transferring frames.
Using Document.elementFromPoint()
is the correct approach. What you are not taking into account is that the HTML is parsed in a hierarchical DOM tree and elementFromPoint()
is not recursive.
You are asking the browser's top-level Document to find an immediate child element at a given X,Y coordinate within the Document. In this case, that is a frame element.
A frame is an embedded window that holds another Document. You need to access the frame's Document and ask it to find a child element at the target X,Y coordinate within the frame. And so on, and so on, until you finally reach the bottom-most child.
Note that elementFromPoint()
takes client coordinates that are relative to the top-left corner of the Document you are calling elementFromPoint()
on. So, when you want to search a child frame's Document, you need to first subtract the frame's own top-left X,Y coordinate (within its parent Document) from the target X,Y coordinate before calling elementFromPoint()
on the frame's Document.