page.click puppeteer 直接到另一个页面不工作
page.click puppeteer direct to another page does not work
我正在尝试进入一个网页,点击登录,这将引导我进入另一个网站(即 Steam)以输入 username/password。但是当我点击引导我进入 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)
我真的需要帮助已经好几天了...
对我来说,你说得太复杂了,尽量保持简单。
此代码工作正常:
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});
无需等待加载事件。
我正在尝试进入一个网页,点击登录,这将引导我进入另一个网站(即 Steam)以输入 username/password。但是当我点击引导我进入 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)
我真的需要帮助已经好几天了...
对我来说,你说得太复杂了,尽量保持简单。
此代码工作正常:
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});
无需等待加载事件。