【问题标题】:Puppeteer route hanging on second run木偶路线挂在第二次运行
【发布时间】:2022-10-24 16:19:54
【问题描述】:

这是我在 Debian 10 VPS 上运行的服务:

require("dotenv").config();
const express = require("express");
const puppeteer = require("puppeteer");
const app = express();

app.use(express.json());

app.post("/create-inbox", (req, res) => {
    if (!req.body["firstName"] || !req.body["lastName"]) return res.status(400).json({ Error: "firstName and lastName are required." });

    let firstName = req.body["firstName"];
    let lastName = req.body["lastName"];

    res.status(202).json({ firstName: firstName, lastName: lastName});    
    createInbox(firstName, lastName);
})

app.listen(process.env.PORT);

async function createInbox(firstName, lastName) {
    const browser = await puppeteer.launch({ headless: true });
    console.log(`Browser opened`);
    const page = await browser.newPage();
    await page.setDefaultNavigationTimeout(0);
    await page.setDefaultTimeout(0);
    await page.goto("https://app.mailparser.io/account/login", {
        waitUntil: 'domcontentloaded'
    });
    console.log(`Loaded ${page.url()}`);
    await page.type("#email", process.env.MP_ACCT);
    await page.type("#password", process.env.MP_PASS);
    await page.click("#start-free-sub");
    await page.waitForNavigation({ waitUntil: "domcontentloaded" });
    console.log(`Loaded ${page.url()}`);
    await page.click("#dashboard_inbox_add");
    await page.type("input[name='label']", `${firstName} ${lastName}`);
    await page.select("select[name='inbox_category_id']", "3015");
    // hangs here on second run
    await Promise.all([
        page.waitForNavigation({ waitUntil: "domcontentloaded" }),
        page.click("#btn_add_address_save"),
    ]);
    console.log("after save");
    await browser.close();
    console.log(`Browser closed`);
}

这似乎在我第一次启动服务时运行得很好,但是当我再次尝试使用该路由时,它就挂在Promise.all(...) 上。 console.log("after save") 只会在第一次运行后出现。

Promise.all(...) 是这里提出的解决方案:Puppeteer hanging on page.click()

以及Puppeteer documentation中的官方解决方案

我不知道下一步该尝试什么。

【问题讨论】:

  • 您是否在单击#start-free-sub 触发的导航附近尝试了Promise.all()?将所有超时设置为无穷大似乎是个坏主意。我会在一两分钟后使这些过期,以便您可以获得清晰的回溯并诊断问题,然后使用 finally 块释放浏览器,这样您就不会泄漏资源。
  • 我还没有尝试将#start-free-sub 和waitForNav 放入Promise.all(),因为它还没有引起任何问题。不过我会测试一下,看看它是否有帮助。我同意无限超时并不理想。一旦我确认一切正常,我确实计划用更明智的东西替换它。我之前确实设置了超时,但堆栈跟踪没有提供任何有用的 afaik。我会再次检查以防万一。感谢您的输入!

标签: express async-await debian puppeteer promise.all


【解决方案1】:

试试 Parseur 吗?它有一个公共 API,可以绕过使用 puppeteer 的需要。

【讨论】:

    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多