如何将 currentapple 变量传递给这个函数?

How to pass the currentapple variable into this function?

我一直在研究这个问题并查看了 Whosebug 上的其他帖子,但他们没有回答我的问题。这是一个独特而具体的问题。我怎样才能让 currentapple 变量成功传递给函数?现在我收到未定义的 src 错误。我正在使用 puppeteer 和 node.js.

进行网络自动化

代码:

    var currentapple = 2;

    console.log(currentapple+" hi1");
    var appleurl= await page.evaluate(async (currentapple) => {
        console.log(currentapple+" hi2");

        var appleelement = await document.getElementsByClassName('ta');
        var appleurl = await appleelement[currentapple].src;
        
        //var gotourl = window.location.href = appleurl;
        //return appleurl;
        
        return await JSON.stringify(appleurl);
    });

错误:

UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'src' of undefined

您必须提供变量作为 page.evaluate 的附加参数:

var currentapple = 2;
var imageurl = await page.evaluate(async (currentapple) => {
    console.log(currentapple+" hi2");
}, currentapple);