Utilizing the Puppeteer page.waitForFunction()
method:
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}
`);
Although it is functional, the code appears unappealing. I am eager to convert it into a multiline format. The official documentation provides a sample snippet:
page.waitForFunction(pageFunction[, options[, ...args]])
pageFunction
<function|string>
Function to be evaluated in browser contextIn order to pass arguments from node.js to the predicate of
page.waitForFunction
function:const selector = '.foo'; await page.waitForFunction(selector => !!document.querySelector(selector), {}, selector);
I attempted the following approach:
console.log(x,y); // 24 42
await page.waitForFunction((x, y) => {
console.log(x, y); // undefined, undefined
if(x && y) {
return true;
}
});
However, the values for x
and y
that I intend to pass are showing as undefined. Can someone point out my mistake?