Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

page.evaluate() - document is undefined when returning it back to Node

I tried to get the Document of a Website with Puppetter but somehow page.evaluate doesnt work the way i thought.

  let browser = await puppeteer.launch({headless:true});
  let page = await browser.newPage();
  await page.setViewport({
    width: 1920,
    height: 1080
  })
  await page.goto('https://www.tradingview.com/chart/');

  const doc = await page.evaluate(() => {
    return document;
  });
  
  console.log(doc) 

When i print doc it prints undefined

like image 337
Timm Nicolaizik Avatar asked Oct 19 '25 12:10

Timm Nicolaizik


1 Answers

Unfortunately, page.evaluate() can only transfer serializable values (roughly, the values JSON can handle). As document returns a DOM element that is not serializable (it contains methods and circular references), it is replaced with undefined. You need to return either serializable value (for example, document.body.innerText) or use something like page.evaluateHandle() and JSHandle API.

like image 52
vsemozhebuty Avatar answered Oct 21 '25 03:10

vsemozhebuty