Page is not defined- nested function
See original GitHub issueTell 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:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
page.evaluate()
calls its callback in the browser context, wherepage
(defined in thepuppeteer
(Node.js) context) is not available. You can return boolean value from this function, check it in thepuppeteer
context and callpage.screenshot()
there.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).