首页 > 解决方案 > page.click puppeteer 直接到另一个页面不起作用

问题描述

我试图去一个网页点击登录,这将引导我到另一个网站(这是 Steam)输入用户名/密码。但是当我点击引导我进行蒸汽时,它只是挂在那里。我的代码:

try {
  await page.goto("https://www.tradeit.gg", {waitUntil: "load", timeout: 0});
} catch(e) {
  console.log(e)
}

try{
  await Promise.all([
    page.waitForSelector('a[href="https://steamtrade.gg/auth/steam"]'),
    page.click('a[href="https://steamtrade.gg/auth/steam"]'),
    page.waitForNavigation({ waitUntil: 'networkidle0' })
])
} catch (e) {
  console.log("can not login, error: " + e);
}

我试图点击的按钮:

<ul class="navbar-nav mr-3" style="color: #c7ced4;">
<a href="https://steamtrade.gg/auth/steam" class="mr-3" onclick="if (!window.__cfRLUnblockHandlers) return false; fireLoginEvent('navbar_mainpage')">
<button class="btn btn-success steamlogin my-2 my-sm-0" type="submit"></button>
</a>
</ul>

我得到的错误:

    can not login, error: TimeoutError: Navigation timeout of 30000 ms exceeded
(node:30861) UnhandledPromiseRejectionWarning: Error: No node found for selector: input[type="password"]
    at Object.exports.assert (/home/danhle/Documents/Work/personalProjects/googleAdd/scrawlingApp/node_modules/puppeteer/lib/cjs/puppeteer/common/assert.js:26:15)
    at DOMWorld.type (/home/danhle/Documents/Work/personalProjects/googleAdd/scrawlingApp/node_modules/puppeteer/lib/cjs/puppeteer/common/DOMWorld.js:306:21)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async /home/danhle/Documents/Work/personalProjects/googleAdd/scrawlingApp/app.js:31:5
(node:30861) 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.jsweb-scrapingpuppeteergoogle-chrome-headless

解决方案


对我来说,你做得太复杂了,尽量保持简单。

这段代码工作得很好:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch({ headless: false });
    const page = await browser.newPage();
    await page.goto('https://www.tradeit.gg', { waitUntil: 'networkidle2' });
    await Promise.all([
        page.waitForNavigation({ waitUntil: 'networkidle2' }),
        page.click('[href="https://steamtrade.gg/auth/steam"]')
    ]); 

    await browser.close();
})();

如果您更改,即使您的代码也有效

await page.goto("https://www.tradeit.gg", {waitUntil: "load", timeout: 0});

以一种您不等待加载事件的方式。


推荐阅读