5

I am using pyppeteer to trigger headless chrome and perform some actions. But first I want all the elements of the web page to load completely. The official documentation of pyppeteer suggests a waitUntil parameter which comes with more than 1 parameters.

My doubt is do i have to pass all the parameters or any one in particular is sufficient? Please suggest if following snippet helps in my case?

await page.goto(url, {'waitUntil' : ['load', 'domcontentloaded', 'networkidle0', 'networkidle2']})
Mahesh
  • 1,117
  • 2
  • 23
  • 42

1 Answers1

7

No, you don't have to pass all possible options to 'waitUntil'. You can pick either of them, or more options at the same time if you like, but if you are:

  • not deailing with a single-page app,
  • not interested in all network connections (like 3rd party trackings for example)

then you are good to go with: 'domcontentloaded' to wait for all the elements to be rendered on the page.

await page.goto(url, {'waitUntil' : 'domcontentloaded'})

The options in details:

  • load: when load event is fired.

  • domcontentloaded: when the DOMContentLoaded event is fired.

  • networkidle0: when there are no more than 0 network connections for at least 500 ms.

  • networkidle2: when there are no more than 2 network connections for at least 500 ms.

[source]

Note: of course it is true for the NodeJs puppeteer library as well, they work the same way in terms of waitUntil.

theDavidBarton
  • 7,643
  • 4
  • 24
  • 51
  • What do you mean by single-page app? the webpage that i am loading will be anyway a single page – Mahesh Jul 12 '20 at 09:14
  • I mean it is e.g. written fully in React.Js or Angular and only functional once all the js bundles are downloaded to the client https://en.wikipedia.org/wiki/Single-page_application. If that is the case you will need to use the `networkidle0` option to make sure your page is fully functional, the `DOMContentLoaded` event may fired earlier than this would happen. – theDavidBarton Jul 12 '20 at 09:19