0

Inside every tr I want to get the date from td:nth-child(2) and compare it with today's date. If they are equal I want to click td:nth-child(5) . I want to do the same process for several tr.
I tried to use loop but I received errors like >Error: ... 'tbody > tr:nth-child(undefined) > td:nth-child(2)' is not a valid selector. OR >ReferenceError: i is not defined

Can anyone help me with this please?

 

  const data = await page.evaluate(() => {
    const date = document.querySelector('tbody > tr:nth-child('+i+') > td:nth-child(2)').innerText;
    return date;
  });
  if (data === today) {
    await page.click('tbody > tr:nth-child('+i+') > td:nth-child(5)');
  }
})();
Ada
  • 5
  • 3

1 Answers1

1

Your i variable is not available inside evaluate function.

You need to pass it as an pageFunction argument.

const data = await page.evaluate((i) => {
    const date = document.querySelector('tbody > tr:nth-child('+i+') > td:nth-child(2)').innerText;
    return date;
  }, i);

page.evaluate documentation

See also How can I pass variable into an evaluate function?

m51
  • 1,950
  • 16
  • 25