How to find HTML elements by text with Cheerio and NodeJS?

by scrapecrow Oct 31, 2022

Using NodeJS' Cheerio we can find any HTML element by partial or exact text value using the :contains() pseudo selector:

const cheerio = require('cheerio');

const $ = cheerio.load(`
    <a>ignore</a>
<a href="http://example.com">link</a>
<a>ignore</a>
`);
console.log(
    $('a:contains("link")').text()
);
"link"

This selector is case sensitive so it might be dangerous to use in web scraping. Instead, it's advised to filter values by text:

const cheerio = require('cheerio');

const $ = cheerio.load(`
    <a>ignore</a>
<a href="http://example.com">Link</a>
<a>ignore</a>
`);

console.log(
    $('a').filter(
        (i, element) => { return $(element).text().toLowerCase().includes("link")}
    ).text()
);
"link"

Related Articles

How to Capture and Convert a Screenshot to PDF

Quick guide on how to effectively capture web screenshots as PDF documents

SCREENSHOTS
PYTHON
NODEJS
How to Capture and Convert a Screenshot to PDF

Playwright Examples for Web Scraping and Automation

Learn Playwright with Python and JavaScript examples for automating browsers like Chromium, WebKit, and Firefox.

PLAYWRIGHT
PYTHON
NODEJS
Playwright Examples for Web Scraping and Automation

Web Scraping with Playwright and JavaScript

Learn about Playwright - a browser automation toolkit for server side Javascript like NodeJS, Deno or Bun.

PLAYWRIGHT
HEADLESS-BROWSER
NODEJS
Web Scraping with Playwright and JavaScript

Guide to Axios Headers

Learn about Javascript's Axios headers. How to configure, update, inspect headers in request and responses, how to set defaults and useful tips

HTTP
NODEJS
Guide to Axios Headers

Axios vs Fetch: Which HTTP Client to Choose in JS?

Explore the differences between Fetch and Axios - two essential HTTP clients in JavaScript - and discover which is best suited for your project.

HTTP
NODEJS
API
Axios vs Fetch: Which HTTP Client to Choose in JS?

Concurrency vs Parallelism

Learn the key differences between Concurrency and Parallelism and how to leverage them in Python and JavaScript to optimize performance in various computational tasks.

SCALING
NODEJS
PYTHON
Concurrency vs Parallelism