【问题标题】:Puppeteer : Chromium hangs when clickingPuppeteer:点击时 Chromium 挂起
【发布时间】:2020-09-13 17:50:42
【问题描述】:

我正在尝试单击一个元素,直到该给定元素从 DOM 中消失,但它似乎使浏览器挂起。

这是一个代码 sn-p ,目标是通过单击“下一步”按钮直到它消失来实际对网站进行分页。(分页结束)

  const jobs = await page.evaluate(
    (container, next) => {
      let next_page = document.querySelector(next);
      while (next_page !== null) {
        next_page.click();
      }
      return true;
    },
    container,
    next
  );

此外,循环似乎不是无限的,因为元素实际上在某个点从 DOM 中消失了。

【问题讨论】:

    标签: node.js web-scraping puppeteer


    【解决方案1】:

    您需要在每次迭代后获取next_page 元素,否则您将永远无法获取null 值以退出while 循环。

    const jobs = await page.evaluate(
      (container, next) => {
        let next_page;
        do {
          next_page = document.querySelector(next);
          next_page.click();
        } while (next_page !== null);
        return true;
      },
      container,
      next
    );
    

    【讨论】:

    • 嗨,Lanken,欢迎来到 S.O.!为了让答案更加清晰,为什么不添加一些代码来说明如何解决相关问题?
    • 谢谢,瓦维洛夫,下次马上做。
    猜你喜欢
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多