puppeteer: [Bug]: Error: Navigating frame was detached

Minimal, reproducible example

import puppeteer from 'puppeteer';
import fs from "node:fs/promises";

// Launch the browser and open a new blank page
const browser = await puppeteer.launch({headless: false,  devtools: true});

const page = await browser.newPage();

const cookies = await fs.readFile("../.cookie.json");
let parse = JSON.parse(cookies.toString());
await browser.setCookie(...parse);

page.setDefaultNavigationTimeout(1000 * 60);
page.setDefaultTimeout(1000 * 60 * 2);
await page.setViewport({width: 1200, height: 1024});
async function task2(){
    let waitForNavigation = page.waitForNavigation();
    await page.goto("https://cn.bing.com");
    await waitForNavigation;
    await delay(1000);
    await page.bringToFront();
    await delay(1000);
    const searchKeywords = [
        "Python 3 最佳实践",
        "JavaScript ES6 新特性",
        "React hooks 使用教程",
        "Pandas 数据分析案例",
        "Matplotlib 可视化图表制作",
        "Scikit-learn 机器学习模型",
        "RESTful API 设计原则",
        "GraphQL 查询语言介绍",
        "Matplotlib 可视化图表制作",
        "Scikit-learn 机器学习模型",
        "RESTful API 设计原则",
        "GraphQL 查询语言介绍",
        "Scikit-learn 机器学习模型",
        "RESTful API 设计原则",
        "GraphQL 查询语言介绍",
        "Matplotlib 可视化图表制作",
        "Scikit-learn 机器学习模型",
        "RESTful API 设计原则",
        "GraphQL 查询语言介绍",
    ];
    for (let searchKeyword of searchKeywords) {
        console.log("handle keyword: ", searchKeyword)
        let waitSearch = page.waitForNavigation();
        await page.locator("#sb_form_q").fill(searchKeyword + `, ${new Date()}`);
        await delay(1000);
        try {
            await page.locatorRace([ page.locator("#search_icon"), page.locator("#sb_search > a"),  ]).click();
        }catch (e) {
            console.error("出错了", e);
            await page.reload();
        }
        await waitSearch;
        await delay(1000);
    }
}

await task2();

await delay(2000);
console.log("task end");
// await browser.close();



function delay(time) {
    return new Promise(resolve => setTimeout(resolve, time));
}

Background

I’m using the latest version, launched using the chrome browser that puppeteer automatically downloads.This shouldn’t be a problem, but when actually running it, the problem occurs frequently and occasionally, especially in long-running programmes where the problem is prone to occur.The probability of recurrence is about 2/5, I think.

            this.#terminationDeferred.resolve(new Error('Navigating frame was detached'));                                                            
                                              ^                                                                                                       
                                                                                                                                                      
Error: Navigating frame was detached  

When the script is started, the window will be closed in about 3s, after which the “TargetCloseError: Protocol error (Page.bringToFront): Session closed.Most likely the page has been closed.” This error has a very low recurrence rate.


Expectation

I want the programme to work.

Reality

During the run, an Error: Navigating frame was detached error occurred, causing the program to terminate.

There was also a ‘targetCloseError’: Protocol error (Page. ringToFront): Session closed. Most likely the page has been closed.

Puppeteer configuration file (if used)

No response

Puppeteer version

23.10.2

Node version

v18.12.1

Package manager

npm

Package manager version

8.19.2

Operating system

Windows

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 40

Most upvoted comments

Locators by design act on a single element. To do things for multiple elements, you need to encode the information about the element into the locator:

await Promise.all([
   page.locator('.myclass:nth-child(1)'),
   page.locator('.myclass:nth-child(2)'),
   page.locator('.myclass:nth-child(3)')
].map(locator => locator.click()))

If it is not possible to encode it in the selector you could try using a function locator with each function identifying a specific element.

Okay.

I do not see the error in that log.

image Please click to view the full document information.