# Wait for an element instead of sleep in Puppeteer

> Three detector rows slept for a time measured once; AmIUnique loaded in 31.7s once and under 10s the next. Polling for the value we read fixed all three.

Source: https://mosaicbrowser.com/blog/wait-for-condition-not-sleep

## A sleep is a guess with a number on it

Three detector rows slept for a time measured once; AmIUnique loaded in 31.7s once and under 10s the next. Polling for the value we read fixed all three.

![31.7s: One page load at 31.7 seconds, the next under 10, beside a screenshot: the Mosaic sidebar with profiles running, each with its own window](https://mosaicbrowser.com/covers/wait-for-condition-not-sleep.webp)

Wait for the thing you are about to read instead of sleeping for a number of seconds. In Puppeteer that means `page.waitForSelector` for an element or `page.waitForFunction` for any condition the page can answer, each with a timeout. We learned it on our own detector runner, where three rows slept for a figure taken from one timing and kept reporting broken pages that were fine.

## Three rows that were measuring the clock

Mosaic's detection suite opens 49 public fingerprint and bot detectors in a profile's real browser and reads each verdict off the page. For a long time a row could say only `settleMs`: open the page, sleep this long, then read. Three rows had their figure set from a single load:

```
pixelscan       20s, measured once at 8s     live it takes 26-34s
amiunique       32s, measured once at 30s    its bundles land at
                                             31.7s on one load and
                                             under 10s on the next
recaptcha-v3    35s, already raised from 20s the score arrives in 3s
                                             or never

```

AmIUnique is the clearest case. Its attribute table is built by four Nuxt bundles, and the page's own resource timings put them at 31.7s, 31.7s and 32.1s on one load and under ten seconds on the next. A 32-second sleep landed on either side of the same site by chance. When it landed early, the suite reported "neither the uniqueness headline nor the attribute table rendered", about a page that renders both.

Pixelscan gave `fail, unread, unread` across three fresh profiles, which looked like a regression in the profiles and was a 20-second sleep on a scan that takes 26 to 34.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Leak tests and detectors](https://mosaicbrowser.com/features/detection-tests)

## Wait for the element's value, not a sleep

Each row now declares a `ready` condition, a JavaScript expression the runner evaluates in the page once a second. The moment it is literally `true`, the run moves on. The old `settleMs` stays, but as a deadline: 90 seconds on all three rows.

The condition is not "the page has loaded" or "a results box exists". It is the fact the extractor is about to parse. For AmIUnique, that is either the headline ("You are unique among the…", or "not unique") or a row of the attribute table with a percentage in it. For reCAPTCHA v3, it is a `score` followed by a number between 0 and 1. Two readings of one fact, in two places, written the same way.

A 90-second deadline sounds slow, and it costs nothing on a fast load, because a page that is ready at four seconds is read at four seconds. The runner logs "ready after 4.0s of a 90s budget", so the record shows how much headroom each row had. A sleep has to be long enough for the slowest load and so charges every fast load for it. A poll charges only the loads that need it.

In Puppeteer the same idea is one call. [`page.waitForFunction`](https://pptr.dev/api/puppeteer.page.waitforfunction) "waits for the provided function, pageFunction, to return a truthy value when evaluated in the page's context", and takes `polling` and `timeout` options:

```
await page.goto('https://amiunique.org/fingerprint')
await page.waitForFunction(
  () => /You are\s*(?:not\s*)?unique among the/i
    .test(document.body.innerText),
  { polling: 1000, timeout: 90_000 }
)
// now read the headline, with the same pattern

```

`waitForSelector` is the right call when the thing you read is an element that appears once and is complete when it does. When the element appears empty and fills in later, a selector wait fires too early, and a function that checks the text inside it does not.

## Two things a timeout should not do

We kept one rule from before: a `ready` that never fires is not turned into a failure by the timer. When the deadline passes, the runner reads the page anyway and lets the extractor say what is missing, in its own words. A timeout raised by the waiting code would replace "the scan had not finished" with "we gave up", which is a fact about our timer presented as one about their site.

And when it does time out, it says where the tab was. On the first run with the new waits, a click-started test on the EFF's Cover Your Tracks timed out and printed `the tab is at https://firstpartysimulator.org/kcarter?try2=true (loading, visible, 0 chars)`. The test had navigated to its own tracker simulator mid-run and that host was serving nothing. "The test was still running" had been hiding a stalled third-party navigation for as long as the row existed. The whole chain is in [what firstpartysimulator.org is](https://mosaicbrowser.com/blog/firstpartysimulator-org).

## The condition has to read a value, not a word

The first `ready` we wrote for that click matched `/bits? of identifying information/` with no number in it. It fired at 42 seconds on the results page's own "How to read your report" section, text the page draws before any measurement lands. Requiring a number before "bits" fixed it, and that is the pattern the extractor already used.

A detector's labels are full of the words you are looking for in sentences that mean the opposite, so the condition should match the verdict's shape (a number, a score, a named state) and not a phrase that might appear in an explanation.

## When waiting longer is the wrong repair

reCAPTCHA v3 had already had its sleep raised once, from 20 to 35 seconds, and a full run still read "no score in the expected 0.0-1.0 range" while a stock browser in the same session scored 0.9. With the clock out of the picture, the row still failed about one load in three. Google's demo page loads two scripts, `api.js` and `enterprise.js`, both define `window.grecaptcha`, and the Enterprise one puts `execute` a level down. Whichever loaded last won:

```
pass    api.js 180ms   enterprise.js 421ms
fail    api.js 156ms   enterprise.js  97ms

```

No sleep length fixes a race between two scripts. A comment above the old 35-second figure had even said it was a guess. Raising it was the wrong repair twice, and polling for the value is what made the real fault visible, because a wait that cannot be the cause any more leaves only the real cause standing.

If a row in your own suite only passes with a longer sleep, ask whether a stock browser on the same machine passes it at all before you change anything. That is the [control that settles "is this ours"](https://mosaicbrowser.com/blog/the-control-is-googles-build). The rows above, with their deadlines and verdicts, are on [detection tests](https://mosaicbrowser.com/features/detection-tests).

## Sources

1. [Puppeteer: Page.waitForFunction() method](https://pptr.dev/api/puppeteer.page.waitforfunction), read November 3, 2026

## Questions

### How do I wait for an element instead of sleeping in Puppeteer?

Use page.waitForSelector for an element, or page.waitForFunction for any condition you can write as a function in the page. Both resolve the moment the condition holds and throw when the timeout passes, so the timeout becomes a deadline instead of a duration.

### What should the wait condition check?

The same thing the next line of your script reads. If you are about to parse a score, wait for a score. Waiting for a generic element such as a results container can fire before the value inside it has arrived.

### How long should the timeout be?

Generous. A polled wait costs nothing on a fast load, because it returns as soon as the condition is true, so the deadline only has to cover the slow loads. We use 90 seconds on detector pages that usually finish in 10 to 35.

### Why did raising the sleep not fix the flaky step?

Because the timing was never the cause on one of our rows. reCAPTCHA's demo page loaded two scripts that both define grecaptcha, and whichever landed last decided whether execute existed. A longer sleep hid nothing and fixed nothing.

## Related

[Detection tests: check a profile against 49 public detectors](https://mosaicbrowser.com/features/detection-tests): Test your anti-detect browser against detectors: Mosaic lists 49 public ones in five groups, from Pixelscan to a reCAPTCHA v3 score, and opens any in a profile.

[Why Cover Your Tracks sends you to firstpartysimulator.org](https://mosaicbrowser.com/blog/firstpartysimulator-org): firstpartysimulator.org is an EFF domain. Cover Your Tracks loads it mid-test as a stand-in first-party site, so a hang there leaves the test running for ever.

[How to use Puppeteer with an anti-detect browser](https://mosaicbrowser.com/guides/use-puppeteer-with-an-anti-detect-browser): Use Puppeteer with an anti-detect browser through puppeteer.connect: start the profile, pass its wsEndpoint, set defaultViewport to null, then disconnect.

[The control that settles "is it our changes?"](https://mosaicbrowser.com/blog/the-control-is-googles-build): An unchanged Chrome for Testing got the same IPHey verdict as our engine, so our changes were not the cause. Its voice list still differs from Chrome's.









[A regex that lost its backslashes inside a template literal](https://mosaicbrowser.com/blog/regex-in-template-literal): A template literal turns \s into s, so a regex shipped inside one matches nothing and throws nothing. It hid five of our detector rows as unread.

[Thirty tabs open: why a test suite needs a fresh tab per site](https://mosaicbrowser.com/blog/one-tab-per-detector): Captcha popups and ad windows left about 30 tabs open in our detector runner. Now each test gets its own tab, and a sweep closes every stray when it ends.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
