0

I made the code below to scrape some data from a webpage. When I execute this message show up in the log console: Error: Evaluation failed: TypeError: Cannot read property 'length' of undefined at puppeteer_evaluation_script:7:39 Browser Closed

This is my code:

const puppeteer = require("puppeteer");
const chalk = require("chalk");
var fs = require("fs");
    
const error = chalk.bold.red;
const success = chalk.keyword("green");
    
(async () => {
      try {
        var browser = await puppeteer.launch({ 
          headless: false,
          defaultViewport: null
         });
        var page = await browser.newPage();
        
        await page.setDefaultNavigationTimeout(0);
        await page.goto('https://synotec.tn/marque/bomi/?ppp=-1');
        
        await page.waitForSelector('h4.product-title');
        var news = await page.evaluate(() => {
            var titleNodeList = document.querySelectorAll('h4.product-title').firstChild;;
            var linkList = document.getElementsByClassName('product-title').firstChild;
            var priceList = document.querySelectorAll('ins');
            var stockStateList = document.querySelectorAll('p.in-stock');
            var titleLinkArray = [];
            for (var i=0; i<titleNodeList.length; i++){
                titleLinkArray[i] = {
                    title: titleNodeList[i].innerText.trim(),
                    link: linkList[i].getAttribute("href"),
                    price: priceList[i].innerText.trim(),
                    stock: stockStateList[i].innerText.trim()
                }       
            }
            return titleLinkArray;
        }
        )
        await browser.close();
        fs.writeFile("bomi.json", JSON.stringify(news), function(err) {
            if (err) throw err;
            console.log("Saved!");
          });
          console.log(success("Browser Closed"));
      } catch (err) {
        console.log(error(err));
        await browser.close();
        console.log(error("Browser Closed"));
      }
    })();
Librairie Synotec
  • 131
  • 1
  • 1
  • 3
  • 1
    NodeList does not have a property `firstChild`. Can you try to explain what you are trying to achieve? – Rajesh Aug 21 '20 at 08:36
  • 1
    The above stands also for `HTMLCollection` returned by `.getElementsByClassName`. – Teemu Aug 21 '20 at 08:37
  • @Rajesh I added firstChild because of a tag within h4. this is the console print without it: Error: Evaluation failed: TypeError: Cannot read property '0' of undefined at __puppeteer_evaluation_script__:10:31 Browser Closed – Librairie Synotec Aug 21 '20 at 08:52
  • Point is `titleNodeList` and `linkList` are undefined. – Rajesh Aug 21 '20 at 08:53
  • I can't understand why are they undefined. This code I used it before and it worked. maybe this because of the DOM. – Librairie Synotec Aug 21 '20 at 09:54
  • No, this code never has worked, maybe you've used `.querySelector(...)`, that returns an element if it exist. Furthermore, it's possible, that `firstChild` returns a textnode, if you specifically expect an element, use `firstElementChild` instead. – Teemu Aug 21 '20 at 11:15

0 Answers0