[英]Issue with puppeteer clicking a dropdown menu
I'm using puppeteer to make a bot that can check for my college homework.我正在使用 puppeteer 制作一个可以检查我的大学作业的机器人。 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.我希望 puppeteer 遵循与检查我是否有任何作业相同的步骤,其中一部分涉及一个下拉菜单,我需要 puppeteer 单击以继续 --- 但是,它不会单击它,而是在终端中给我一个错误。
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 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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.