Can I use XPath selectors in Cheerio?

No, you can not use XPath selectors in Cheerio . According to these GitHub issues , there is no plan to support XPaths in Cheerio.

GitHub Issue

However, if you simply want to work with XML documents and parse those using Cheerio, it is possible. Here is some sample code for parsing XML using Cheerio.

const cheerio = require('cheerio');
const xml = `
  <bookstore>
    <book category="web">
      <title lang="en">Practical Python Projects</title>
      <author>Yasoob Khalid</author>
      <year>2022</year>
      <price>39.95</price>
    </book>
    <book category="web">
      <title lang="en">Intermediate Python</title>
      <author>Yasoob Khalid</author>
      <year>2018</year>
      <price>29.99</price>
    </book>
  </bookstore>
`;

// Load the XML document as a Cheerio object
const $ = cheerio.load(xml, { xml: true });

// Select all book titles 
const titles = $('book > title');

// Print the text content of each title
titles.each((i, title) => {
    console.log($(title).text());
});

// Output:
// Practical Python Projects
// Intermediate Python

Related Cheerio web scraping questions: