如何将 JSHandle 和本机对象 (String) 传递给 Puppeteer 评估函数?

How can one pass both a JSHandle and a native object (String) to Puppeteer evaluate functions?

我可以很好地传递本机对象(字符串、列表、数字等)或 JSHandle(感谢@hardkoded),但不知道如何同时低音

这是我现在的代码,正文中硬编码了字符串 (deployName):

async clickClientDeploymentsActionLink(deployName) {
    const rowsHandle = await this.getRows();
    await rowsHandle.evaluate(node => { 
        // need to figure HOW to pass both a string and a JSHandle
        let deployName = 'Distributed Selene 8.2';
        var children = []
        for(x = 0; x < node.length; x++) {
            if (node[x].childElementCount > 0) {
                children.push(node[x]);
            }
        }
        let childNode = null
        for(y = 0; y < children.length; y++) {
            if (children[y].innerText.includes(deployName)) {
                childNode = children[y];
                break
            }
        } 
        let actionNode = null;
        for(z = 0; z < childNode.childNodes.length; z++) {
            if (childNode.childNodes[z].innerText == 'Actions') {
                actionNode = childNode.childNodes[z];
                break;
            }
        }
        actionNode.click()
    });
}

这非常有效,但我还需要弄清楚如何传递字符串变量,而且似乎无法弄清楚句柄和变量的传递方式不同(我如何传递字符串等变量的示例,这完全有效):

    async rowsChildrenCount(id='dashboardGrid') {
        const children = await this.page.evaluate(({id}) => { 
            const grid = document.getElementById(id)
            const rows = grid.getElementsByClassName('ag-row')
            var children = []
            for(x = 0; x < rows.length; x++) {
                if (rows[x].childElementCount > 0) {
                    children.push(rows[x].childElementCount);
                }
            }
            return children
        }, {id});

更新:hardkoded 提出了一个解决方案(我之前做过的,但是在评估中添加了“(节点,项目名称)”),但它似乎没有用,

    async clickItemActionLink(itemName) {
        const rowsHandle = await this.getRows();
        await rowsHandle.evaluate((node, itemName) => { 
            var children = []
            for(x = 0; x < node.length; x++) {
                if (node[x].childElementCount > 0) {
                    children.push(node[x]);
                }
            }
            let childNode = null
            for(y = 0; y < children.length; y++) {
                if (children[y].innerText.includes(itemName)) {
                    childNode = children[y];
                    break
                }
            } 
            let actionNode = null;
            for(z = 0; z < childNode.childNodes.length; z++) {
                if (childNode.childNodes[z].innerText == 'Actions') {
                    actionNode = childNode.childNodes[z];
                    break;
                }
            }
            actionNode.click()
        });
    }

得到“评估失败:TypeError:无法读取 属性 'childNodes' of null”,因为 itemName 为 null。

您可以构建一个函数,其中第一个参数始终是 JSHandle,但其余参数是您可以传递给评估函数的值。

await page.goto("");
const title = await page.$(".js-products-menu");
await title.evaluate((node, text) => node.innerText = text, "Foo");