Skip to content

page.waitForFunction gets "lost" on navigation - original Puppeteer does not have this problem #129

Closed

Description

@ckcr4lyf

I think this is similar to #38 maybe , but I have a reproducible example.

Basically if you create a promise via page.waitForFunction to await a condition before an action like form submission, tha texecution context of that gets silently lost.

A new page.waitForFunction after the navigation with same condition works as expected; and with puppeteer even the old one correctly evaluates and resolves.

Expected

When run w/ puppeteer:

$ node index.mjs 
opened https://quotes.toscrape.com/login — Quotes to Scrape
✅ New promise fullfilled correctly!
✅ Original, OLD promise fullfilled correctly!

Actual

When run w/ rebrowser:

$ node index.mjs 
opened https://quotes.toscrape.com/login — Quotes to Scrape
✅ New promise fullfilled correctly!
❌ Original, OLD promise rejected! {
  status: 'rejected',
  reason: TimeoutError: Waiting failed: 30000ms exceeded
      at new WaitTask (file:///home/raghu/Documents/node_modules/puppeteer-core/lib/esm/puppeteer/common/WaitTask.js:45:34)
      at IsolatedWorld.waitForFunction (file:///home/raghu/Documents/node_modules/puppeteer-core/lib/esm/puppeteer/api/Realm.js:22:26)
      at CdpFrame.waitForFunction (file:///home/raghu/Documents/node_modules/puppeteer-core/lib/esm/puppeteer/api/Frame.js:584:43)
      at CdpFrame.<anonymous> (file:///home/raghu/Documents/node_modules/puppeteer-core/lib/esm/puppeteer/util/decorators.js:101:27)
      at CdpPage.waitForFunction (file:///home/raghu/Documents/dnode_modules/puppeteer-core/lib/esm/puppeteer/api/Page.js:1435:37)
      at file:///home/raghu/Documents/rebrowser/index.mjs:16:27

Code to reproduce (comment / replace the library to test both).

import puppeteer from 'rebrowser-puppeteer-core';
// import puppeteer from 'puppeteer';

const browser = await puppeteer.launch({
  executablePath: process.env.CHROME_PATH || '/usr/bin/chromium',
  headless: false,
});

try {
  const page = (await browser.pages())[0];
  // sleep for 2 seconds
  await new Promise(resolve => setTimeout(resolve, 2000));
  await page.goto('https://quotes.toscrape.com/login', { waitUntil: 'networkidle0' });
  console.log(`opened ${page.url()} — ${await page.title()}`);

  const oldPromise = page.waitForFunction(() => {
    console.log(`[OLD] evaluating...`);
    let element = document.querySelector('h2');
    console.log(`[OLD] element: ${element?.textContent}`);
    if (element === null){
      return false;
    }

    return element.textContent === 'Top Ten tags';
  });

  await new Promise(resolve => setTimeout(resolve, 2000));
  await page.type('input[name="username"]', 'admin');
  await page.type('input[name="password"]', 'password');
  await page.click('input[type="submit"]');

  await new Promise(resolve => setTimeout(resolve, 2000));
  const newPromise = page.waitForFunction(() => {
    console.log(`[NEW] evaluating...`);
    let element = document.querySelector('h2');
    console.log(`[NEW] element: ${element?.textContent}`);
    if (element === null){
      return false;
    }

    return element.textContent === 'Top Ten tags';
  });

  const [oldResult, newResult] = await Promise.allSettled([oldPromise, newPromise]);

  if (newResult.status === 'rejected'){
    console.error(`New promise rejected, broken script?`, newResult);
    process.exit(1);
  } else {
    console.log(`✅ New promise fullfilled correctly!`);
  }

  if (oldResult.status === 'fulfilled'){
    console.log(`✅ Original, OLD promise fullfilled correctly!`);
  } else {
    console.error(`❌ Original, OLD promise rejected!`, oldResult);
  }
} finally {
  await browser.close();
}

Activity

  1. ckcr4lyf commented on Sep 17, 2026

    @ckcr4lyf
    Author

    Note: in error trace it shows puppeteer-core , thats coz I have an override set for that to rebrowser:

    "puppeteer-core": "npm:rebrowser-puppeteer-core@^24.8.1"
    
  2. rebrowsernet commented on Oct 18, 2026

    @rebrowsernet
    Collaborator

    Thanks for the clean repro. Confirmed on 24.8.1 and 25.14.0: after a navigation the patch cleared the page's context but never restarted pending waitForFunction() / waitForSelector() tasks, which stock Puppeteer does. With #132, your script resolves both promises. It's also a regression test now. Ships in the next release.

  3. rebrowsernet commented on Oct 18, 2026

    @rebrowsernet
    Collaborator

    Released in v1.1.1: rebrowser-puppeteer-core / rebrowser-puppeteer 25.14.100, rebrowser-playwright-core / rebrowser-playwright 1.64.100, and rebrowser-playwright 1.64.100 on PyPI. (npm doesn't allow re-publishing 25.14.0 / 1.64.1, so this fix release uses .100.) waitForFunction() / waitForSelector() started before a navigation now resolve in the new document. Your script passes. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions