[英]Issue with puppeteer clicking a dropdown menu
我正在使用 puppeteer 制作一個可以檢查我的大學作業的機器人。 我希望 puppeteer 遵循與檢查我是否有任何作業相同的步驟,其中一部分涉及一個下拉菜單,我需要 puppeteer 單擊以繼續 --- 但是,它不會單擊它,而是在終端中給我一個錯誤。
這是我編寫的代碼(這不是完整的代碼,但這是發生錯誤的第一點,因此解決它可以防止問題發生):
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();
這是我通過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.
這可能是什么原因造成的?
您嘗試在元素加載之前單擊。 改成:
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.