Set a variable in input
I want to use puppeteer to login faster and i made this script, but it recognize the variable of the input as a variable of the site
let data = { email : 'mail', password : 'passwd' } async function login(data) { page.evaluate(() => { document.querySelector('#loginlb > div.c-loginlb__popup > div > form > div:nth-child(1) > input').value = data.email; }); }
The error is this: Evaluation failed: TypeError: Cannot read property ’email’ of undefined
How can i fix that?
You should pass arguments after evaluate arrow function. like this page.evaluate(pageFunction, ...pageFunction arguments)
. docs.
let data = { email : 'mail', password : 'passwd' } async function login(_data) { page.evaluate((data) => { document.querySelector('#loginlb > div.c-loginlb__popup > div > form > div:nth-child(1) > input').value = data.email; },_data); }
Since data
is not within the scope in page.evaluate
, you can set it in global
and retrieve it with global
since it’ll always be in scope.
global['data'] = { email : 'mail', password : 'passwd' } async function login(data) { page.evaluate(() => { document.querySelector('#loginlb > div.c-loginlb__popup > div > form > div:nth-child(1) > input').value = global['data'].email; }); }