waitForNavigation挂起,即使页面已加载

时间:2018-03-22 07:24:32

标签: puppeteer

我正在尝试使用puppeteer自动登录网站,使用以下脚本:

const puppeteer = require('puppeteer');
async function logIn(userName, password) {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://foo.com'); //anonymized host
    await page.type('[name="Email"]', userName);
    await page.type('[name="Pass"]', password);
    page.click('[type=submit]');
    await page.waitForNavigation({waitUntil: 'load'});
}

logIn('user@domain.com')

await的{​​{1}}最终会在30秒时超时。在同一个脚本上用waitForNavigation启动木偶操作,我可以检查铬的devtools,{headless: false}在超时的thresold间隔之前逐渐“完成”。我做错了吗?

2 个答案:

答案 0 :(得分:11)

单击提交并等待导航的

According to Puppeteer documentation正确模式如下:

registerTempTable()

答案 1 :(得分:1)

除了Everettss的答案,我想提出一个更现代的代码。基本上,不执行任何操作并调用waitFornavigation,而是采取另一种方法。像这样:

//Get the promise which isn't resolved yet
const navigationPromise = page.waitForNavigation(); 

//Perform the activity (Click, Goto etc). Note that the link is any valid url
await page.goto(link);

//wait until the navigationPromise resolves
await navigationPromise;

这有助于避免根据this的讨论而出现种族状况

此代码利用了异步/等待,这比基于承诺的代码更具可读性