Shin6
Shin6

Reputation: 3

How to fix ' variable in loof with innerText'

error in this //2 code. Evaluation failed: TypeError: Cannot read property 'innerText' of undefined..

i want to crawling website .tbl_column > table > tbody > tr > td[1,2,3,4,5,6...] in loof how to fix..?

os is mac

installed puppeteer

//1 - was worked well 
for(var i= 0; i< 12; i++){
      var value = await page.evaluate(() => document.querySelectorAll('.tbl_column > table > tbody > tr > td')[2].innerText); 
      console.log(value);
    }



//2 -  this is error syntax "i"
for(var i= 0; i< 12; i++){
      var value = await page.evaluate(() => document.querySelectorAll('.tbl_column > table > tbody > tr > td')[i].innerText); 
      console.log(value);
    }

Upvotes: 0

Views: 72

Answers (1)

jfriend00
jfriend00

Reputation: 707158

That error will occur when there are less than 12 items in your document.querySelectorAll() result object.

Your for loop doesn't really make sense to me anyway. You should be getting the result from document.querySelectorAll(), getting the length of the result you get and using a for loop based on that length value.

This also seems like something you could do some elemental debugging on by just doing a console.log() of the value before you do [i].innerText on it.

I don't know puppeteer well, but you can try this:

let value = await page.evaluate(() => {
    let items = document.querySelectorAll('.tbl_column > table > tbody > tr > td');
    let results = [];
    for (let i = 0; i < items.length; i++) {
        let data = items[i].innerText;
        console.log(data);
        results.push(data);
    }
    return results;
});
console.log(value);

Upvotes: 1

Related Questions