Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
How will I get the content ie the title of a tag while web scrapping with puppeteer?

I am following this tutorial. https://www.digitalocean.com/community/tutorials/how-to-scrape-a-website-using-node-js-and-puppeteer

I am trying to learn how to use puppeteer.

I am trying to get whats written in the anchor tag .

So my code in pageScraper file is this

const scraperObject = {
    url: 'http://books.toscrape.com',
    async scraper(browser){
        let page = await browser.newPage();
        console.log(`Navigating to ${this.url}...`);
        // Navigate to the selected page
        await page.goto(this.url);
        // Wait for the required DOM to be rendered
        await page.waitForSelector('.page_inner');      

        let mytext=await page.$$eval('.nav-list li > a',text => text.textContent)
        
        console.log(mytext);
    }
}//         dataObj['bookTitle'] = await newPage.$eval('.product_main > h1', text => text.textContent);
module.exports = scraperObject;

However I am getting an undefined while printing.

I am trying to get this. Selectors

book text

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, http://books.toscrape.com doesn't have any functional JS, so there's no need for Puppeteer. Better to just use a lightweight static request library like fetch or axios and an HTML parser like Cheerio.

That said, for pedagogical purposes,

await page.$$eval('.nav-list li > a',text => text.textContent)

appears to be the problem. $$eval means "select all and run a callback on all selected elements in the browser context", similar to document.querySelectorAll. So the variable you call text is an array of elements, not a single element, and the array property .textContent is undefined.

If you want to select a single element, use $eval. Please call the variable element or el, not text.

On the other hand, if you want to select multiple elements, treat it like an array in the callback.

Getting one element's text:

await page.$eval('.nav-list li > a', el => el.textContent);

Getting multiple elements' text:

await page.$$eval('.nav-list li > a', els => els.map(el => el.textContent));
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!