Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS Puppeteer wait for page load to complete [duplicate]

After seeing this youtube video using puppeteer I got inspired to play a bit around with it. But I seem to have made the wrong choice of a website as a starter project.

const puppeteer = require('puppeteer')
;(async () => {
  const browser = await puppeteer.launch()
  const page = await browser.newPage()

  await page.goto('http://www.produktresume.dk/AppBuilder/search?page=0')
  page.once('load', () => {
    const drugs = page
      .evaluate(() =>
        [...document.querySelectorAll('div.entity-link')].map(item => item)
      )
      .catch(err => console.log(err))
    console.log(drugs[0])
  })

  await browser.close()
})()

I have googled around and lost track of the different things I have tried..

My perception of my problem is that I don't call the evaluate at the right time - when the page is loaded.

like image 770
Norfeldt Avatar asked Jan 28 '23 08:01

Norfeldt


1 Answers

There is absolutely no need to use page.on('load') to find if page loaded.

You can use the,

  • waitUntil option on .goto call.
  • waitForSelector function for specific selector.

Usage,

await page.goto('http://www.produktresume.dk/AppBuilder/search?page=0', {waitUntil: 'networkidle0'});
await page.waitForSelector("#wrapper"); // Found on the page source code, wait for this to appear
// the rest is just as usual
const drugs = await page
.evaluate(() =>
 [...document.querySelectorAll('div.entity-link')].map(item => item)
)
.catch(err => console.log(err))
console.log(drugs[0])

Make sure to use await for the .evaluate call.

like image 172
Md. Abu Taher Avatar answered Jan 31 '23 23:01

Md. Abu Taher