繁体   English   中英

puppeteer 单击下拉菜单时出现问题

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM