I'm trying to scrape a bunch of tables on a page with Puppeteer but the amount of tables varies each time and therefore i need to pass a variable into the page.evaluate() function.
I have an array containing the variables i need to use but lets say i wanna check the second table manually using a variable, here's how i approached the problem initially:
const x = 2
let value = await page.evaluate(() =>
document.querySelector("#pageBody > div:nth-child(" + x + ") > table > tr > td").innerText
);
console.log(value);
//Evaluation failed: ReferenceError: x is not defined
After some research,from what i understand, the correct way of passing a variable in there is:
const x = 2;
let value = await page.evaluate((x) => {
document.querySelector("#pageBody > div:nth-child(" + x + ") > table > tr > td").innerText
}, x);
console.log(value);
//value = undefined
But since im not getting the results i want, there's something im doing wrong or not understanding properly. Any ideas?