0

I have a newbie question.

For some reason my code can't access the value stored in the keyword: this.Selector (last line of code). I think it must have something to do that it's a callback function but I don't what and how to change it.

 class Test {
    constructor(url, range, Selector){
      this.url = url;
      this.range = range;
      this.Selector = Selector
    }

async puppi(){
    const browser =  await puppeteer.launch({headless: true});
    const page =  await browser.newPage();
    await page.goto(this.url);

    const Voucher =  await page.evaluate(()=>{
    let GrapCoupons = document.querySelectorAll(this.Selector);

If you know why, then it would mean a lot to me since I am totally stucked.

Thanks in advance!!

  • `this` works differently inside of an arrow function. It references whatever the value of `this` is in the calling scope. If you use an anonymous function, it will work. – Scott Marcus Apr 12 '22 at 20:06
  • @ScottMarcus Actually that's not the explanation. `this` in an arrow function in the `puppi` method *does* refer to the instance, and `this.Selector` would work. The problem is rather that the function passed to `evaluate` is serialised to text by Puppeteer. – Bergi Apr 12 '22 at 21:03

0 Answers0