How to find elements by CSS selector in Puppeteer?

CSS selectors are one of the most popular ways to parse HTML pages when web scraping. In NodeJS and Puppeteer, CSS selectors can be used through the page.$ and page.$$ methods:

const puppeteer = require('puppeteer');

async function run() {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto("");

    // to get the first matching element:
    await page.$("p");
    // to get ALL matching elements:
    await page.$$("p");

    // we can also modify the captured elements immediatly:
    // get the text value:
    await page.$eval("p", element => element.innerText);
    // get attributes attribute:
    await page.$eval("a", element => element.href);

    // same with multiple elements, like count total appearances:
    await page.$$eval("p", elements => elements.length)



⚠ It's possible that these commands will try to find elements before the page has fully loaded if it's a dynamic javascript page. For more see How to wait for a page to load in Puppeteer?

Also see: How to find elements by XPath in Puppeteer?

Question tagged: Puppeteer, Headless Browsers, Data Parsing

Related Posts

How to Scrape With Headless Firefox

Discover how to use headless Firefox with Selenium, Playwright, and Puppeteer for web scraping, including practical examples for each library.

How to Use Chrome Extensions with Playwright, Puppeteer and Selenium

In this article, we'll explore different useful Chrome extensions for web scraping. We'll also explain how to install Chrome extensions with various headless browser libraries, such as Selenium, Playwright and Puppeteer.

Web Scraping With Puppeteer - 2024 Puppeteer Tutorial

Introduction to using Puppeteer in Nodejs for web scraping dynamic web pages and web apps. Tips and tricks, best practices and example project.