简体   繁体   中英

Puppeteer How to check if the page has redirected

I am trying to check with waitForNavigation() to see if the page switches or not. 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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