Hugo Del-Negro
Hugo Del-Negro

Reputation: 143

In puppeteer page.evaluate can't find text

I am trying to retrieve "document.querySelector(".navigator__results").childNodes[0]" to my variable result

const result = await page.evaluate( async () => {
    const a = await document.querySelector(".navigator__results").childNodes[0];
    console.log(a);
    return a
});
console.log(result);

The console.log(a) on the browser works. But the console.log(result) doesn't

Any ideas how to tackle this issue ?

Have tried to delay it as shown bellow, but no success:

     await page.waitFor(10000);
     const result = await page.evaluate( async () => {
        function sleep(ms) {
            return new Promise(resolve => setTimeout(resolve, ms));
        }
        await sleep(8000);
        console.log(document.querySelector(".navigator__results").childNodes[0]);
        const a = await document.querySelector(".navigator__results").childNodes[0];
        console.log(a);
        return a;

     });
     console.log(result);

Upvotes: 1

Views: 379

Answers (1)

vsemozhebuty
vsemozhebuty

Reputation: 13822

document.querySelector(".navigator__results").childNodes[0] returns HTML element. You can log it in the browser console, but you cannot retrieve it via page.evaluate(): page.evaluate() can transfer only serializable data (roughly speaking — the data JSON can handle), and HTML element can't be serialized, so undefined is returned.

Also, you don't need await for document.querySelector() :)

Upvotes: 1

Related Questions