NodeJS Puppeteer "ReferenceError: page is not defined"
NodeJS Puppeteer "ReferenceError: page is not defined"
我遇到了与这段特定代码相关的问题:
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(arr[0]["link"]);
sleep(5000);
const claim = await page.evaluate(() => {
它向我发送了这个错误(最上面的最后一行是第 23 行):
C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221
throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails));
^
Error: Evaluation failed: ReferenceError: page is not defined
at __puppeteer_evaluation_script__:4:13
at ExecutionContext._evaluateInternal (C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ExecutionContext.evaluate (C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async C:\Users\root\Documents\Workspace\NodeJS Scrapper\part2.js:23:19
这是我的完整代码:
const puppeteer = require('puppeteer');
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
const fs = require('fs');
const data = fs.readFileSync('./games.txt', 'utf8');
const arr = JSON.parse(data);
console.log(arr);
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(arr[0]["link"]);
sleep(5000);
const claim = await page.evaluate(() => {
let claim = [];
if(document.querySelector('div.css-xvql1u button.css-n9sjaa span').textContent == "Continuer") {
page.click("div.css-xvql1u button.css-n9sjaa");
}
sleep(15000);
return claim;
});
await browser.close();
console.log(claim);
})();
文件“games.txt”包含这个数组:
[{"title":"Mages of Mystralia","link":"https://www.epicgames.com/store/fr/p/mages-of-mystralia"}]
你能帮我解决这个问题吗?
好了,问题就上线了:
page.click("div.css-xvql1u button.css-n9sjaa");
page.evaluate
运行您在网页上下文中提供的功能。 (意思是上面闭包中的变量 page
没有定义。)
您应该将该行替换为类似以下内容:
document.querySelector("div.css-xvql1u button.css-n9sjaa").click()
或者,如果您真的坚持使用 page
标识符或人偶脚本中的任何其他值,您可以将其作为参数传递给函数。 (check here for an example)
我遇到了与这段特定代码相关的问题:
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(arr[0]["link"]);
sleep(5000);
const claim = await page.evaluate(() => {
它向我发送了这个错误(最上面的最后一行是第 23 行):
C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221
throw new Error('Evaluation failed: ' + helper_js_1.helper.getExceptionMessage(exceptionDetails));
^
Error: Evaluation failed: ReferenceError: page is not defined
at __puppeteer_evaluation_script__:4:13
at ExecutionContext._evaluateInternal (C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async ExecutionContext.evaluate (C:\Users\root\Documents\Workspace\NodeJS Scrapper\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
at async C:\Users\root\Documents\Workspace\NodeJS Scrapper\part2.js:23:19
这是我的完整代码:
const puppeteer = require('puppeteer');
function sleep(milliseconds) {
const date = Date.now();
let currentDate = null;
do {
currentDate = Date.now();
} while (currentDate - date < milliseconds);
}
const fs = require('fs');
const data = fs.readFileSync('./games.txt', 'utf8');
const arr = JSON.parse(data);
console.log(arr);
(async() => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
await page.goto(arr[0]["link"]);
sleep(5000);
const claim = await page.evaluate(() => {
let claim = [];
if(document.querySelector('div.css-xvql1u button.css-n9sjaa span').textContent == "Continuer") {
page.click("div.css-xvql1u button.css-n9sjaa");
}
sleep(15000);
return claim;
});
await browser.close();
console.log(claim);
})();
文件“games.txt”包含这个数组:
[{"title":"Mages of Mystralia","link":"https://www.epicgames.com/store/fr/p/mages-of-mystralia"}]
你能帮我解决这个问题吗?
好了,问题就上线了:
page.click("div.css-xvql1u button.css-n9sjaa");
page.evaluate
运行您在网页上下文中提供的功能。 (意思是上面闭包中的变量 page
没有定义。)
您应该将该行替换为类似以下内容:
document.querySelector("div.css-xvql1u button.css-n9sjaa").click()
或者,如果您真的坚持使用 page
标识符或人偶脚本中的任何其他值,您可以将其作为参数传递给函数。 (check here for an example)