Skip to content
Advertisement

How can I tell that the page has finished loading?

I’m playing with Chromium’s headless web browser API. Based on chrome_remote_shell source code, I came up with the following code:

JavaScript

Here’s example output:

JavaScript

I noticed that I get a long stream of messages, last one of them being Network.loadingFinished, but I got this one for multiple requestIds. How can I modify my script so that it terminates when the page fully loaded and I can escape the loop?

Advertisement

Answer

It turns out I should have also subscribed to page events via Page.enable:

JavaScript

What we’re doing here is enabling notifications for both Page and Network items, then opening the website and reading all messages that happen after. Once we reach Page.loadEventFired, we can assume that the page finished loading, which is when we can exit the loop and carry out any actions that depend on this condition.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement