chois
chois

Reputation: 1

Evaluation failed: ReferenceError occurs when call evaluate function

When you call evaluate in puppeteer, an error occurs as follows. 'Evaluation failed: ReferenceError: cov_mjds8ir32 is not defined'

also 'page.$eval' occurs same error

page = await browser.newPage();

await page.goto("https://myurl.com",{timeout: 5000, waitUntil: "networkidle2"});
await page.evaluate(() => {
     console.log("log");
});

const userid = 'myid';
page = await browser.newPage();

await page.goto("https://myurl.com",{timeout: 5000, waitUntil: "networkidle2"});
await page.$eval('#input[name=userid]', el => el.value = userid);

Upvotes: 0

Views: 73

Answers (2)

chois
chois

Reputation: 1

Thanks for the answer Yaroslavm. In my case, I had to do it using a template string.

The code below as an example

await page.evaluate(() => {
     console.log("log");
});

After making this change, it ran normally.

await page.evaluate(`(async() => {
  console.log('1');
})()`);

The reference link is below: https://github.com/puppeteer/puppeteer/issues/1665#issuecomment-354241717

Upvotes: 0

Yaroslavm
Yaroslavm

Reputation: 4804

First of all, your selector, I guess, input[name=userid] and not #input[name=userid].

$eval signature is

$eval<Selector extends string, Params extends unknown[], 
Func extends EvaluateFuncWith<NodeFor<Selector>, Params> = 
EvaluateFuncWith<NodeFor<Selector>, Params>>(selector: Selector, 
pageFunction: Func | string, ...args: Params)

where pageFunction is Function and ...args is params that can be passed to that function.

Function itself has it's own scope of visibility. It's executed in page context and page context doesn't know anything about test context.

To make function see vars from test context, you should pass them as a function argument.

await page.$eval('input[name=userid]', (el, _userId) => el.value = _userid, userId);

Upvotes: 0

Related Questions