Evaluate JavaScript
Learn how to use Puppeteer’s evaluate function to execute JavaScript code within the context of a web page.
The evaluate
function in Puppeteer allows us to run JavaScript code within the context of a web page. It provides a powerful way to interact with the page's DOM and extract information. We can pass a function as an argument to the evaluate function that will be executed within the page's context. Below is the syntax for using the evaluate
function:
const result = await page.evaluate(() => {// Implement the logic here// Return the result});
Use cases of the evaluate
function
The following are some common use cases where we must use the evaluate
function in web scraping. After reviewing these examples, we'll better understand the scenarios in which Puppeteer's evaluate function is appropriate.
Extracting text and data
The most common use case of evaluate
function is to retrieve text content, extract additional element attributes such as hyperlinks, or filter any child elements under a specific element. For example, we can extract product names, prices, ratings, or other relevant information from an e-commerce website. Let’s see this in action in the code snippet below:
{ "name": "puppeteer-learn", "version": "1.0.0", "description": "", "type": "commonjs", "main": "index.js", "scripts": { "start": "node index.js" }, "keywords": [], "author": "", "license": "MIT", "dependencies": { "puppeteer": "19.8.3" } }
In the above code snippet,
Lines 18–57: We define selectors for different elements.
Lines 59–78: We use the
evaluate
function with those selectors to extract the necessary information.
Transforming data
In addition to extracting data, we can perform transformations using the evaluate function. Below are a few common examples.
Convert date formats: The ...