Search code examples
javascriptpuppeteerreload

How to reload a page using puppeteer(if page doesn't load only)


Most of the time I launch my automated bot it stucks at the beginning due to network issues,so when I reload it,It works.I want my bot to do this if the page doesn't load


Solution

  • Your page request isn't responded or the response isn't success with status 200 OK.
    So maybe you want to get the page requested again if the response isn't OK.
    These below lines will try to reload the page until it's status is 200 OK.

    const gotoHome = async () => {
        try {
            let response
            do {
                response = await page.goto('https://www.google.com', {
                    timeout: 0,
                    waitUntil: 'load'
                })
            } while (response.status() !== 200)
        } catch (error) {
            (async () => await bot.gotoHome())()
        }
    }
    
    

    Please accept my answer as right if this help you.