I know is a noob question, but I want to know when I should use page.evaluate
I also know the documentation exists, but I still do not understand
Can anybody give me an explanation about how and when to use this function when creating a scraper with puppeteer?
Evaluates a function in the browser context. If the function, passed to the frame. evaluate, returns a Promise, then frame. evaluate would wait for the promise to resolve and return its value. If the function passed into frame.
page.$$eval(selector, pageFunction[, ...args])This method runs Array. from(document. querySelectorAll(selector)) within the page and passes it as the first argument to pageFunction . If pageFunction returns a Promise, then page. $$eval would wait for the promise to resolve and return its value.
Puppeteer has page. title() function to get the title of the current page.
First, it is important to understand that there are two main environments:
You should use page.evaluate()
when you are seeking to interact with the page directly in the page DOM environment by passing a function and returning a <
Promise
<
Serializable
>>
which resolves to the return value of the passed function.
Otherwise, if you do not use page.evaluate()
, you will be dealing with elements as an ElementHandle
object in the Node.js (Puppeteer) environment.
Example Usage:
const example = await page.evaluate(() => {
const elements = document.getElementsByClassName('example');
const result = [];
document.title = 'New Title';
for (let i = 0; i < elements.length; i++) {
result.push(elements[i].textContent);
}
return JSON.stringify(result);
});
See the simplified diagram below:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With