0
function clickElement(page, selector, textTarget) {
  page.evaluate(() => {
    [...document.querySelectorAll(selector)]
      .find((element) => element.textContent === textTarget)
      .click();
      console.log(selector,textTarget)
// selector and textTarget is undefined
  });
}

page.evaluate cannot access block scope variables, selector and textTarget in this example.

  • 2
    Welcome to SO! (1) the logs show up in the browser console, not Node, unless you add a listener for them. See [How do print the console output of the page in puppeter as it would appear in the browser?](https://stackoverflow.com/questions/58089425). (2) you need to pass the arguments to `page.evaluate` otherwise they won't be available in the browser context and will be undefined. See [How can I pass variable into an evaluate function?](https://stackoverflow.com/questions/46088351/how-can-i-pass-variable-into-an-evaluate-function) – ggorlen Jun 23 '21 at 14:36
  • 1
    Does this answer your question? [How can I pass variable into an evaluate function?](https://stackoverflow.com/questions/46088351/how-can-i-pass-variable-into-an-evaluate-function) – Vaviloff Jun 24 '21 at 04:33
  • Can you please show me the code how can i access any variable inside page.evaluate() block. – Simar Bagga 18 Jun 24 '21 at 15:12

0 Answers0