I'm using puppeteer to make a bot that can check for my college homework. I want puppeteer to follow the same steps that I do to check if I have any homework, and part of this involves a dropdown menu that I need puppeteer to click in order to continue --- however, it doesn't click it and instead gives me an error in the terminal.
This is the code I wrote (this is not the full code but this is the first point where the error occurred so solving it will prevent the issue):
const puppeteer = require("puppeteer");
async function checkHomeWork() {
const browser = puppeteer.launch({ headless: false, defaultViewport: null });
const page = (await browser).newPage();
const url = "https://www.jce.ac.il/";
(await page).goto(url);
(await page).waitForTimeout(10000);
const dropDown = (await page).$$("#infostation-dd-trigger");
(await dropDown).click();
(await page).waitForTimeout(5000);
}
checkHomeWork();
This is the error I get in the terminal when running this code via node app.js
:
(node:16992) UnhandledPromiseRejectionWarning: Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (C:\Users\milad\Desktop\New folder\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:261:23)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext._evaluateInternal (C:\Users\milad\Desktop\New folder\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:161:64)
at async C:\Users\milad\Desktop\New folder\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:102:30
at async DOMWorld.$$ (C:\Users\milad\Desktop\New folder\node_modules\puppeteer\lib\cjs\puppeteer\common\DOMWorld.js:122:26)
at async checkHomeWork (C:\Users\milad\Desktop\New folder\app.js:12:6)
(node:16992) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16992) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
What could be causing this?
You try to click before the element has been loaded. Change to:
const puppeteer = require('puppeteer');
puppeteer.launch({ headless: false, defaultViewport: null }).then(async browser => {
const page = await browser.newPage();
const url = "https://www.jce.ac.il/";
await page.goto(url);
page
.waitForSelector('#infostation-dd-trigger')
.then(() => {
// Rest of the logic here.
});
browser.close();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.