I'm trying to run a code like this:
const login = "login";
await page.$eval('#LoginForm_username', el => el.value = login);
It enters a login
value inside a form field.
However, I keep getting a weird error:
Error: Evaluation failed: ReferenceError: login is not defined at puppeteer_evaluation_script:1:19 at ExecutionContext._evaluateInternal (/home/denis/WEB/nlu/node_modules/puppeteer/lib/ExecutionContext.js:122:13) at process._tickCallback (internal/process/next_tick.js:68:7) -- ASYNC -- at ExecutionContext. (/home/denis/WEB/nlu/node_modules/puppeteer/lib/helper.js:111:15) at ElementHandle.$eval (/home/denis/WEB/nlu/node_modules/puppeteer/lib/JSHandle.js:436:50) at process._tickCallback (internal/process/next_tick.js:68:7) -- ASYNC -- at ElementHandle. (/home/denis/WEB/nlu/node_modules/puppeteer/lib/helper.js:111:15) at DOMWorld.$eval (/home/denis/WEB/nlu/node_modules/puppeteer/lib/DOMWorld.js:156:21) at process._tickCallback (internal/process/next_tick.js:68:7) -- ASYNC -- at Frame. (/home/denis/WEB/nlu/node_modules/puppeteer/lib/helper.js:111:15) at Page.$eval (/home/denis/WEB/nlu/node_modules/puppeteer/lib/Page.js:347:29) at Page. (/home/denis/WEB/nlu/node_modules/puppeteer/lib/helper.js:112:23) at crawlForSchedule (/home/denis/WEB/nlu/crawler.js:99:16) at process._tickCallback (internal/process/next_tick.js:68:7)
As far as I understand, it means that the login
variable is out of browser context or something like this.
I've found that it's possible to pass a variable using the page.evaluate()
function like this:
const links = await page.evaluate((evalVar) => {
console.log(evalVar); // should be defined now
}, evalVar);
but how to do something like this with the page.$eval()
function? Or how can I achieve the needed behavior (form field input) using the page.evaluate()
function?