Vivien Pipo Vivien Pipo - 28 days ago 8
Javascript Question

Puppeteer: interact with dom in waitForSelector handler

When I use puppeteer js web crawler, in

waitForSelector
handler I can use
console.log
without trouble:

page
.waitForSelector('input[value=update]')
.then(() => {
console.log('this is');
console.log('it');
});


But I have an error when I want to interact with DOM:

page
.waitForSelector('input[value=update]')
.then(() => {
const inputValidate = await page.$('input[value=update]');
});


This code triggers this error:


const inputValidate = await page.$('input[value=update]');
^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:537:28)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Function.Module.runMain (module.js:609:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:598:3


Do you know how to intercat with DOM in a
waitForSelector
handler without trigger an error?

Answer Source
page
  .waitForSelector('input[value=update]')
  .then(async() => {
    const inputValidate = await page.$('input[value=update]');
  });