gpt4 book ai didi

javascript - 如何将 JSHandle 和 native 对象(字符串)传递给 Puppeteer 评估函数?

转载 作者:太空宇宙 更新时间:2023-11-03 22:04:07 25 4
gpt4 key购买 nike

我可以很好地传递 native 对象(字符串、列表、数字等)或 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建议了一个解决方案(我之前所做的,但在评估中添加了“(node,itemName)”),但它似乎不起作用,

    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()
});
}

由于 itemName 为 null,因此出现“评估失败:TypeError:无法读取 null 的属性“childNodes””。

最佳答案

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

await page.goto("https://stackoverflow.com/questions/59204241/how-can-one-pass-both-a-jshandle-and-a-native-object-string-to-puppeteer-evalu");
const title = await page.$(".js-products-menu");
await title.evaluate((node, text) => node.innerText = text, "Foo");

关于javascript - 如何将 JSHandle 和 native 对象(字符串)传递给 Puppeteer 评估函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59204241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com