繁体   English   中英

Puppeteer 如何检查页面是否已重定向

[英]Puppeteer How to check if the page has redirected

我正在尝试使用waitForNavigation()检查页面是否切换。 这是我正在使用的代码

        if(await page.waitForNavigation() == null ){
            console.log('False')
            await browser.close()
        }else {
            console.log('True')
            await browser.close()    
        }

例如,让我们说屏幕上有一个框,然后您会被重定向到另一个页面。 我希望 True 或 False 在 console.log 上弹出。

我不得不稍微改变一下答案,但在这里

        if(newUrl == startingURL){
            console.log('False');
            await browser.close() 
        }else{
            console.log('True')
            await browser.close() 
        }

If you only care about redirects, like HTTP redirects & window.location.href redirects, you can check the URL that you end up on and compare it to the URL you started with:

const startURL = 'https://example.com/redirect-start'
await page.goto(startURL)
if (page.url() === startURL) { console.log('False') }
else { console.log('True') }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM