木偶多行waitForFunction方法

我正在使用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 42

await page.waitForFunction((x, y) => {

    console.log(x, y); // undefined, undefined

    if(x && y) {

        return true;

    }

});

但x和y这我试图通过是不确定的。我做错了什么?


慕的地10843
浏览 506回答 2
2回答

千万里不及你

您需要传递x和y-当前它们是参数。您还可以稍微简化一下功能:await&nbsp;page.waitForFunction((x,&nbsp;y)&nbsp;=>&nbsp;(x&nbsp;&&&nbsp;y&nbsp;?&nbsp;true&nbsp;:&nbsp;undefined),&nbsp;{},&nbsp;x,&nbsp;y);

一只甜甜圈

您需要将x和y参数传递给waitForFunction。console.log(x,y); // 24 42await page.waitForFunction((x, y) => {&nbsp; &nbsp; console.log(x, y); // undefined, undefined&nbsp; &nbsp; if(x && y) {&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}, {}, x, y);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript