问题描述
我试图让 puppeteer 在继续下一个语句之前等待导航完成.基于 Docs for waitForNavigation() ,代码应该在下面工作.但它只是跳到下一个语句,我必须使用一种解决方法来等待响应中的特定 URL.
I am trying to get puppeteer to wait for the navigation to finish before moving on to the next statement. Based on the Docs for waitForNavigation() , the code should work below. but it just skips to the next statement and I have to use a workaround to wait for a specific URL in the response.
我也尝试了所有等待选项
(加载,domcontentloaded,networkidle0 和 networkidle2).
I have tried all the waituntil options as well
( load, domcontentloaded, networkidle0 and networkidle2 ) .
感谢我如何让它正常工作的任何想法.
Any ideas how I could get that working properly is appreciated.
const browser = await puppeteer.launch({
headless: false,
})
const page = await browser.newPage()
const home = page.waitForNavigation()
await page.goto(loginUrl)
await home
const login = page.waitForNavigation()
await page.type('#email', config.get('login'))
await page.type('#password', config.get('password'))
await page.click('#submitButton')
await login // << skips over this
// the following line is my workaround and it works , but ideally I don't want
// to specify the expected "after" page each time I navigate
await page.waitForResponse(request => request.url() === 'http://example.com/expectedurl')
推荐答案
page.waitForNavigation()
等待导航开始和结束.
The function page.waitForNavigation()
waits for navigation to begin and end.
导航已经通过启动page.click()
.
The navigation has already been initiated with page.click()
.
因此,您可以使用Promise.all()
避免上述函数之间的竞争条件:
Therefore, you can use Promise.all()
to avoid the race condition between the mentioned functions:
const browser = await puppeteer.launch({
headless: false,
});
const page = await browser.newPage();
await page.goto(loginUrl);
await page.type('#email', config.get('login'));
await page.type('#password', config.get('password'));
await Promise.all([
page.click('#submitButton'),
page.waitForNavigation({
waitUntil: 'networkidle0',
}),
]);
await browser.close();
这篇关于单击后如何让 Puppeteer waitForNavigation 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!