【问题标题】:Issue with puppeteer clicking a dropdown menupuppeteer 单击下拉菜单时出现问题
【发布时间】:2020-11-28 15:09:53
【问题描述】:

我正在使用 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.

这可能是什么原因造成的?

【问题讨论】:

    标签: javascript json npm puppeteer


    【解决方案1】:

    您尝试在元素加载之前点击。 改为:

    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();
    });
    

    【讨论】:

    • 现在我收到一个类型错误,说 page.$ 不是函数
    • .then() 不允许我在其中执行任何操作,我无法进行点击调用,也无法声明函数,也无法使用页面描述选择器。$
    • 请问有其他方法解决这个问题吗
    猜你喜欢
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多