我正在使用Puppeteerpage.waitForFunction()方法:await page.waitForFunction(` (window.hero.x - 1 === ${x} || window.hero.x === ${x} || window.hero.x + 1 === ${x} || window.hero.x === ${x}) && (window.hero.y - 1 === ${y} || window.hero.y === ${y} || window.hero.y + 1 === ${y} || window.hero.y === ${y}) || window.hero.x === ${x} && window.hero.y === ${y}`);它正在工作,但看起来很糟糕。我想将其更改为多行。在文档中有:page.waitForFunction(pageFunction[, options[, ...args]])pageFunction<function|string>在浏览器上下文中要评估的功能要将参数从node.js传递给page.waitForFunction函数谓词:const selector = '.foo';await page.waitForFunction(selector => !!document.querySelector(selector), {}, selector);我这样尝试过:console.log(x,y); // 24 42await page.waitForFunction((x, y) => { console.log(x, y); // undefined, undefined if(x && y) { return true; }});但x和y这我试图通过是不确定的。我做错了什么?
2 回答

千万里不及你
TA贡献1784条经验 获得超9个赞
您需要传递x
和y
-当前它们是参数。您还可以稍微简化一下功能:
await page.waitForFunction((x, y) => (x && y ? true : undefined), {}, x, y);

一只甜甜圈
TA贡献1836条经验 获得超5个赞
您需要将x和y参数传递给waitForFunction。
console.log(x,y); // 24 42
await page.waitForFunction((x, y) => {
console.log(x, y); // undefined, undefined
if(x && y) {
return true;
}
}, {}, x, y);
添加回答
举报
0/150
提交
取消