0

i dont understand how to use module in page.evaluate, i also find this post on SO : How to pass required module object to puppeteer page.evaluate SO

but i dont understand how to use it, i have had :

await page.addScriptTag({ path: './node_modules/fs.realpath/index.js'});

i got :

Error: Evaluation failed: TypeError: fs.appendFileSync is not a function

const fs = require('fs');

page.evaluate(()=>{
        let elements = document.querySelectorAll("a.myclass.vid");

        elements.forEach((element, index) => {
            fs.appendFileSync("textresult.txt", element.textContent+"\r\n");
        })

    });

thanks for your explication :o)

1 Answers1

0

That anonymous function you pass to page.evaluate() runs in the context of the web page puppeteer is running in, so of course it doesn't know what fs is. You need to return something from that anonymous function, and then call fs.appendFileSync with that:

const texts = await page.evaluate(() => {
  return document.querySelectorAll('a.myclass.vid').map((el) => {
    return el.textContent + "\r\n"
  })
})

fs.appendFileSync('textresult.txt', texts)
user2926055
  • 1,963
  • 11
  • 10
  • i also use your idea, but "map" return : _Error: Evaluation failed: TypeErr or: document.querySelectorAll(...).map is not a function_ i used this syntax and it works : `const TitresVideos = await page.evaluate(() => { let names = document.querySelectorAll( "a.myclass.video" ); let arr = Array.prototype.slice.call(names); let text_arr = []; for (let i = 0; i < arr.length; i += 1) { text_arr.push($vartraited+"\r\n"); } return text_arr; })` – VeilleurTryToFix Apr 29 '20 at 07:41
  • Hi @VeilleurTryToFix , [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll) returns a nodeList, not an array. In order to use array methods like `.map` or `.forEach`, I usually use the [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) operator as follows: `[...document.querySelectorAll( "a.myclass.video" )]` you can check it [here](https://stackoverflow.com/questions/47585409/use-spread-operator-on-nodelist-in-typescript) – charly rl Apr 29 '20 at 08:31
  • Yep sorry, forgot about the return type of `querySelectorAll`, thanks @charlyrl – user2926055 Apr 29 '20 at 19:07