Reputation: 23
I am using these two methods to get the text of an HTML element. For example:
let elHandle = await page.$("#product");
let productName = await elHandle.evaluate(element => {
return element.textContent;
});
And
let productName = await page.$eval("#product", element => {
return element.textContent;
});
In both examples I pass the "pageFunction" as parameter to both methods. But what is a page function in puppeteer?
Edit: I should add that the following passage is given in the documentation but I don't understand what it means:
pageFunction <function|string> Function to be evaluated in the page context
Edit: typo!
Upvotes: 2
Views: 604
Reputation: 103
Your Puppeteer script is running in NodeJS. NodeJS lauches the browser and goes through your script. If you use console.log('hello')
in your script it will log it to your terminal using NodeJS.
A page function is a function that Puppeteer (through NodeJS) turns into a string, sends to the browser, and then runs inside the Chromium instance. If you use console.log('hello')
in your page function, it will log it to the console in the browser.
Example based on yours
console.log('hello') // Displayed in terminal running your script.
page.$eval("#product", element => {
// We are now inside the browser and can use DOM APIs!
console.log('hello') // Displayed in browser's console
});
Upvotes: 2