Saurabh
Saurabh

Reputation: 940

Puppeteer - variable not getting passed in the evaluate function

I have a function where i am trying to get all the text on the child elements from the parent node.

getTextsForChildElements: async function (selector) {
    const data = await page.evaluate(() => {
        const tds = Array.from(document.querySelectorAll(selector))
        return tds.map(td => td.textContent)
    },selector);
    console.log(data);
},

However, I am seeing selector is not definederror.

Tried using this question as help, but didn't resolve. any idea, how i can pass the selector.

Upvotes: 0

Views: 153

Answers (1)

yeniv
yeniv

Reputation: 1639

The anonymous function which is the first argument to the page.evaluate should accept the arguments which you are passing. Right not, it it not taking any arguments. Modify it as below:

getTextsForChildElements: async function (selector) {
    const data = await page.evaluate((selector) => {
        const tds = Array.from(document.querySelectorAll(selector))
        return tds.map(td => td.textContent)
    },selector);
    console.log(data);
},

Upvotes: 1

Related Questions