简体   繁体   English

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

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

I am trying to check with waitForNavigation() to see if the page switches or not.我正在尝试使用waitForNavigation()检查页面是否切换。 Here is the code I am using这是我正在使用的代码

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

For example let us just say a box on the screen is there and you get redirected to another page.例如,让我们说屏幕上有一个框,然后您会被重定向到另一个页面。 I want True or False to pop up on the console.log.我希望 True 或 False 在 console.log 上弹出。

I had to switch up the answer a bit but here it is我不得不稍微改变一下答案,但在这里

        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: 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