OiO.lk Blog javascript puppeteer nodejs, cant take the value outside of the evaluate scope
javascript

puppeteer nodejs, cant take the value outside of the evaluate scope


I have a puppeteer program, which i get the element of a div, and then process its children.
When I try to log with console.log(), I can inspect and see the value on browser. Yet on node js, console, I can’t see the value that was supposed to be pushed to the array.

This is a longer program, but i just post the code of the function where my problem is related. What is the reason to this ? I also checked How can I pass variable into an evaluate function? to interact outside scope variables with .evaluate()..
if the console.log() gives the correct value, that means the value is obtained, but the push method fails. And I dont see a warning or a error on both browser or program log. What is the reason ?

async function observer(){
    await page.waitForFunction("document.querySelectorAll(\"div[id='myid']\").length > 1")
    var arrayOfMailNames = []
    await page.evaluate((arrayOfMailNames) => {
        var parentDiv = document.querySelectorAll("div[id='myid']");
        for(var t = 0; t < parentDiv.length; t++){
                if(parentDiv[t].getAttribute('id') !=null){
                    console.log(` ${parentDiv[t].split(";")[0]}`); //returns correct values on the browser's console. 
                    arrayOfMailNames.push(parentDiv[t].split(";")[0]); //apparently doesnt work
                }
        }
    }, arrayOfMailNames)
    console.log(`${arrayOfMailNames.length}`); //logs 0 on nodejs terminal.. which is not correct because I used push above
}



You need to sign in to view this answers

Exit mobile version