Unexpected Behavior of page.waitForFunction in npm Puppeteer

Using Puppeteer, the expected action from waitForFunction isn’t triggered despite conditions being true.

page.waitForExpression("document.querySelector('#alert').innerText.includes('success')").then(() => processError(404));
page.waitForElement('.output', { timeout: 150000, visible: true }).then(handleResult);

How can these behaviors be synchronized?

I experienced a similar challenge with Puppeteer where waitForFunction resolved prematurely due to its internal polling mechanism. From my experience, the condition was sometimes evaluated before the necessary DOM changes had fully manifested, which led to unexpected early resolutions. One solution I found was to use a custom function that ensured all dynamic content was loaded before proceeding. It also helped to double-check that the page’s events had fully fired, which could be achieved by wrapping your condition within more robust checks that include page readiness states.