Javascript 如何将JSHandle和本机对象(字符串)传递给Puppeter函数?
我可以传递本机对象(字符串、列表、数字等)或JSHandle(感谢@hardkoded),但不知道如何同时传递这两个对象 这是我现在的代码,正文中硬编码了字符串(deployName):Javascript 如何将JSHandle和本机对象(字符串)传递给Puppeter函数?,javascript,node.js,puppeteer,jest-puppeteer,Javascript,Node.js,Puppeteer,Jest Puppeteer,我可以传递本机对象(字符串、列表、数字等)或JSHandle(感谢@hardkoded),但不知道如何同时传递这两个对象 这是我现在的代码,正文中硬编码了字符串(deployName): async clickClientDeploymentsActionLink(deployName) { const rowsHandle = await this.getRows(); await rowsHandle.evaluate(node => { // nee
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()
});
}
异步clickClientDeploymentsActionLink(deployName){
const rowsHandle=wait this.getRows();
等待rowsHandle.evaluate(节点=>{
//需要考虑如何同时传递字符串和JSHandle
让deployName='Distributed Selene 8.2';
var children=[]
对于(x=0;x0){
push(节点[x]);
}
}
设childNode=null
对于(y=0;y
这非常有效,但我需要弄清楚如何传递字符串变量,而且似乎无法弄清楚如何传递,因为句柄和变量的传递方式不同(我如何传递字符串之类的变量的示例,完全有效):
异步rowsChildrenCount(id='dashboardGrid'){
const children=等待此.page.evaluate({id})=>{
const grid=document.getElementById(id)
const rows=grid.getElementsByClassName('ag-row')
var children=[]
对于(x=0;x 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()
});
}
异步clickItemActionLink(itemName){
const rowsHandle=wait this.getRows();
等待rowsHandle.evaluate((节点,项名称)=>{
var children=[]
对于(x=0;x0){
push(节点[x]);
}
}
设childNode=null
对于(y=0;y
由于itemName为null,因此获取“求值失败:TypeError:无法读取null的属性'childNodes'”您可以构建一个函数,其中第一个参数始终是
JSHandle
,但其余参数是可以传递给求值函数的值
等待页面。转到(“https://stackoverflow.com/questions/59204241/how-can-one-pass-both-a-jshandle-and-a-native-object-string-to-puppeteer-evalu");
const title=等待页面。$(“.js产品菜单”);
等待title.evaluate((node,text)=>node.innerText=text,“Foo”);
Hey hardkoded!谢谢你的回复,但是我恐怕我已经试过了,没有用:`const rowsHandle=wait this.getRows();wait rowsHandle.evaluate((node,itemName)=>{……})`由于itemName为null,我得到了一个错误。我更新了上面的原始帖子,以正确地显示此代码,因为我无法正确地将其放在这里。您需要将itemName传递给求值函数,就像我使用“Foo”时所做的那样。该死,我一定是越来越笨了!没有注意到(由于缺少{})这个“Foo”是您示例中通过的变量。再次感谢!如果我再次回来(针对不同的问题),我可能必须开始向您支付救世主费用…好的,除了投票支持您的回答外,一个“向上投票”的人(实际上我是新提问题的人)如何准确投票?我已经将其标记为正确的一个(所以我不会错过做任何我应该做的事情)。是的,我已经马上做了“除了投票支持你的回答”。
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()
});
}