I am currently learning how to make web requests using JavaScript with puppeteer.
After some trial and error, I was able to extract the value of a tag from a random website.
However, I am struggling to figure out how to retrieve 10 consecutive values of tag a using a "for" loop.
This is the code that successfully extracted the value:
const result0 = await page.evaluate(() => {
return document.querySelectorAll('.ui.divided.list > a')[0].textContent;
});
console.log(`The value is ${result0}`);
Here is my attempt at using a "for" loop (which is not working due to the error "ReferenceError: i is not defined"):
for (var i = 0; i < 9; i++) {
const result = await page.evaluate(() => {
return document.querySelectorAll('.ui.divided.list > a')[i].textContent;
});
console.log(`The value is ${result}`);
}
I don't want to repeat the same code and use ([1], [2], [3], ...), so how can I achieve this?
Thank you
EDIT: I FORGOT TO MENTION THAT I'M UNABLE TO PRINT THE RESULT USING querySelectorAll AS IT RETURNS UNDEFINED. FOR EXAMPLE:
const result = await page.evaluate(() => {
return document.querySelectorAll('.ui.divided.list > a').textContent;
});
console.log(`The value is ${result[0]}`);