我可以传递原生对象(字符串、列表、数字等)或 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 为空,因此得到“评估失败:TypeError:无法读取 null 的属性 'childNodes' ”。