I have to go to the page enter the appropriate data for login and password and once I click the registration button on the page I want to reload the page do location.reload(), but after doing this it pops me an error because the items have not yet had time to load. I don't know how to fix this, anyone have any ideas?
Error:Error: Execution context was destroyed, most likely because of a navigation.
async function launchBrowser() {
    const browser = await puppeteer.launch({
        executablePath: 'C:/Program Files/BraveSoftware/Brave-Browser/Application/brave.exe',
        slowMo: 10,
        headless: false,
        devtools: false,
    })
    const pages = await browser.pages()
    const page = pages[0]
    const url = 'www.example.pl'
    while (true) {
        try {
            await page.goto(url, { timeout: 90000, waitUntil: 'networkidle2' })
            break
        } catch (error) {
            if (error.message.includes('ERR_NETWORK_CHANGED')) {
                console.error('Network changed. Retrying...')
            } else {
                console.error('An error occurred:', error)
                break
            }
        }
    }
    return page
}
async function test(){
    const page = await launchBrowser()
    await main(page)
}
async function main(page) {
    
    await page.type('#login', 'logdasdin')
    await page.type('#password', 'password')
    await page.type('#password2', 'password')
    await page.evaluate(async () => {
        const registerBtn = document.querySelectorAll('button')[3]
        registerBtn.click()
        location.reload()
       
    })
    await main(page)
}
test()
 
    