WebEngine.load() does not wait until loading has completed. That means if you try to access the web page DOM immediately after load() returns, the page is incompletely loaded.
The right way to do it is to access the DOM after the page has completed loading. The component notifies completion via a listener. It should look like this (from
WebEngine documentation):
The other problem in your code is the line:
This code does not find "body" elements in the web page loaded in WebEngine. Instead, what it does is look for a "body" element in the scene's object graph.
When a WebEngine loads a web page, it does not convert the HTML elements in web page into JavaFX Nodes.
One correct way to do what you want is: