可以在使用 Puppeteer 登录后,使用 page.url()
方法获取当前页面的 URL,并与预期的主页 URL 进行比较来判断是否已经跳转到主页。例如:
if (page.url() === 'https://example.com/home') {
console.log('Successfully logged in and redirected to home page.');
} else {
console.log('Login failed or not redirected to home page.');
}
或者
awaitpage.waitForNavigation({waitUntil:'load'});
if (page.url() === 'https://example.com/home') {
console.log('Successfully logged in and redirected to home page.');
} else {
console.log('Login failed or not redirected to home page.');
}
这里使用 waitForNavigation 等待页面跳转完成后在进行断言,等待直到页面跳转并且整个资源加载完成