Sometimes "Error: Execution context was destroyed, most likely because of a navigation."

Viewed 4818

I have this code:

(async function(){
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();
try{
    await page.goto('https://erp2.webmaissistemas.com.br/csp/e8p7g7q/system/www/login.csp');

    await page.click('#UserName')
    await page.keyboard.type('123');

    await page.click('#Password')
    await page.keyboard.type('123');

    await page.click('#botaoLogin')

    await page.waitForNavigation()

    let form = await page.$('#form')
    if (form){
        let content = await form.$eval('.alert-danger', element => element.textContent)
        throw content
    }
    console.log('OK')
}
catch (e){
    console.log('Error: ', e)
}
})()

Sometimes, give "Error: Execution context was destroyed, most likely because of a navigation."

Whats is solution?

1 Answers

If I remember correctly (you can search puppeteer issues in GitHub for this error message), this is a common, but a hardly catchable issue with some sites and navigation in the code. You can minimize it 1) with reducing race condition danger for click and navigation events and 2) with additional waiting for some time after the navigation or with waiting for some selector:

'use strict';

const puppeteer = require('puppeteer');

(async function main() {
  const browser = await puppeteer.launch({ headless: false });
  const page = await browser.newPage();
  try {
    await page.goto('https://erp2.webmaissistemas.com.br/csp/e8p7g7q/system/www/login.csp');

    await page.click('#UserName');
    await page.keyboard.type('123');

    await page.click('#Password');
    await page.keyboard.type('123');

    await Promise.all([
      page.waitForNavigation(),
      page.click('#botaoLogin'),
    ]);

    await page.waitForSelector('#form');

    const form = await page.$('#form');
    if (form) {
      const content = await form.$eval('.alert-danger', element => element.textContent);
      throw content;
    }
    console.log('OK');
  } catch (e) {
    console.log('Error: ', e);
  }
})();

Related