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?

Add Comment
2 Answer(s)

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); } 
Answered on July 15, 2020.
Add Comment

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;   }); } 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.