Reputation: 3631
I'm using page.$eval
in Puppeteer and I dont know why a pageFunction
would return an empty object when the object isn't empty. Here's a code sample:
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch({
headless: false,
slowMo: 1000
});
const page = await browser.newPage();
await page.goto('https://www.google.com/search?q=news');
const result1 = await page.$eval('#resultStats', elem => elem.textContent)
console.log('result1', result1); // returns 'About 2,890,000,000 results (0.45 seconds)'. This is expected behavior straight from puppeeteer docs
const result2 = await page.$eval('#resultStats', elem => elem)
console.log('result2', result2); // returns and empty object. Why? I would have expected to see a DOM Node Object here
await browser.close();
})();
How do I get the whole element back in result2?
Upvotes: 0
Views: 1051
Reputation: 467
Well, as you said above.You can get the dom node in the evaluate function.But when you return the dom node from evaluate function, puppeteer will handle the data you returned.So you can't get the adjective data.
Upvotes: 0
Reputation: 3631
I didn't understand that the pageFunction
function is running within Chromium itself, so in the second example where it is returning elem => elem
, it's actually returning a live NodeList collection to Puppeteer.
But returning a live NodeList collection from Chromium back to puppeteer isn't possible because the way Puppeteer passes data to and from Chromium has to be serializable via JSON.stringify
/ JSON.parse
. When Puppeteer runs JSON.stringify
on a live NodeList, I believe it returns an empty object.
Upvotes: 1