question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Evaluation failed: ReferenceError in waitForFunction

See original GitHub issue

When calling the following code:

[...]
const options = {
	page: await browser.newPage()
};
await utils.fetchPage("someurl", ".some-selector", options);

with the following fetchPage implementation:

async function fetchPage(p_url, p_waitForSelectors, p_options) {
	assert(p_options.hasOwnProperty('page'));

	await p_options.page.goto(p_url, {waitUntil: 'networkidle2'});

	if (p_waitForSelectors) {
		await p_options.page.waitForFunction(() =>
			document.querySelectorAll(p_waitForSelectors).length
		);
	}

	const html = await p_options.page.content();
	assert(html);
	return html;
}

I get the error:

[17:39:49] [ERROR]  Error: Evaluation failed: ReferenceError: p_waitForSelectors is not defined
    at eval (eval at waitForPredicatePageFunction (:2:23), <anonymous>:4:30)
    at eval (eval at waitForPredicatePageFunction (:2:23), <anonymous>:4:57)
    at onRaf (<anonymous>:49:35)
    at pollRaf (<anonymous>:42:15)
    at waitForPredicatePageFunction (<anonymous>:7:22)
    at ExecutionContext._evaluateInternal (\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async WaitTask.rerun \node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:528:23)

When replacing p_waitForSelectors with the explicit string literal ".some-selector" all works fine:

[...]
		await p_options.page.waitForFunction(() =>
			document.querySelectorAll(".some-selector").length
		);
[...]

Is this some weird JavaScript quirk that I somehow misunderstand? Or is this a genuine bug?

I’m using Puppeteer 5.5.0.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MarByteBeepcommented, Nov 25, 2020

That fixed it! @vsemozhetbyt thanks so much for the help!

0reactions
cloydlaucommented, Mar 2, 2021

It finally works by adding:

const fnStr = fn.toString()
  .replace(/\/\* istanbul ignore next \*\//g, '')
  .replace(/cov_.+\(\)\..+]\+\+;/g, '')
  .replace(/cov_.+\(\)\..+]\+\+,/g, '')

new Function('return ' + fnStr)()()

Now I’m working on how to get the result of fn

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Evaluation failed: ReferenceError: res is not defined
Official document - The method returns a Promise which resolves to the return value of pageFunction. This mean you can get back a...
Read more >
Evaluation failed: ReferenceError: req is not defined". Im using ...
I get the error "Error: Evaluation failed: ReferenceError: req is not defined". Im using puppeteer and im trying to get the clients input...
Read more >
Puppeteer Evaluation failed: ReferenceError: __awaiter is ...
I am using Puppeteer with TypeScript and when I try to evaluate an async function on a page: await page.evaluate(async function() { //...
Read more >
Page.evaluate() method - Puppeteer
Evaluates a function in the page's context and returns the result. If the function passed to page.evaluateHandle returns a Promise, the function will...
Read more >
Waiting for text to display on a page with Puppeteer
Here is a (pseudo-code) solution to this problem: ... The core of this solution leverages Puppeteer's waitForFunction in conjunction with a ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found