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.

Page is not defined- nested function

See original GitHub issue

Tell us about your environment:

  • Puppeteer version: 5.7.1
  • Platform / OS version: Sierra 10.12.4
  • URLs (if applicable): https://google.com
  • Node.js version: v8.9.4

Please include code that reproduces the issue.

const puppeteer = require('puppeteer');
async function getPic() {
  const browser = await puppeteer.launch( { headless: false});
  const page = await browser.newPage();
  await page.goto('https://google.com');
  await page.evaluate( async () => {
    const imageslink = document.querySelector("a[data-pid='2']");
 if (imageslink.textContent === "Images") {
    imageslink.click();
    await page.screenshot({path: 'google.png'});
 }
  });

  await browser.close();
}

getPic();

What is the expected result? I would expect the script to make a screenshot because the if condition is true. And with page.evaluate being a nested function it should have access to the outer function’s( getpic()'s) variables (in this case page), should it not ?

What happens instead? instead I get an error saying "page is not defined.

**note the only reason I’m trying to write this script is for practice purposes

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
vsemozhetbytcommented, Apr 4, 2018

page.evaluate() calls its callback in the browser context, where page (defined in the puppeteer (Node.js) context) is not available. You can return boolean value from this function, check it in the puppeteer context and call page.screenshot() there.

0reactions
vsemozhetbytcommented, Apr 4, 2018

In the page.evaluate() parameter function you can use any JavaScript syntax and globals you usually use in the browser scripts. You can return any value that can be safely serialized/deserialized (primitives, simple data structures).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript nested function not defined - Stack Overflow
Javascript nested function not defined · 1. Compare the example and your code and check for differences. · 1. You defined a function...
Read more >
Python Inner Functions: What Are They Good For?
Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct...
Read more >
Nested Functions - MATLAB & Simulink - MathWorks
A nested function is a function that is completely contained within a parent function. Any function in a program file can include a...
Read more >
Function Gotchas - Learning Python [Book] - O'Reilly
The nested function has access only to its own local scope, the global scope in the enclosing module, and the built-in names scope....
Read more >
Nested Functions in JavaScript - TekTutorialsHub
The nested function can access all variables, functions & arguments defined in the containing function. This ability where the inner function ...
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