Puppeteer: Fehler: Der Ausführungskontext wurde zerstört, höchstwahrscheinlich aufgrund einer NavigationJavaScript

Javascript-Forum
Anonymous
 Puppeteer: Fehler: Der Ausführungskontext wurde zerstört, höchstwahrscheinlich aufgrund einer Navigation

Post by Anonymous »

Ich habe ein Problem mit diesem Code. Ich verwende Puppeter und es scheint den Fehler auszulösen:

„Fehler: Ausführungskontext wurde zerstört, höchstwahrscheinlich aufgrund einer
Navigation.“

Warum konnte das passieren? Ich hänge ein Fragment meines Codes an, wo es anscheinend fehlschlägt.

Code: Select all

export async function goTo(page: Page, target1: string, target2: string) {
await common.click(page, '#nav-item-welcome');
await common.waitTillHTMLRendered(page);

await common.click(page, target1, { clickType: 'puppeteer' }); // clicktype puppeter or jquery
await common.waitTillHTMLRendered(page);

await common.click(page, target2, { clickType: 'puppeteer' });
await common.waitTillHTMLRendered(page);
}
Klickfunktion:

Code: Select all

export async function click(el: Frame | Page, selector: string, options?: {
visible?: boolean;
hidden?: boolean;
timeout?: number;
enabled?: boolean;
clickType?: 'jQuery' | 'puppeteer';
text?: string;
}) {
const isJQuery = await el.evaluate(() => typeof $ == 'function');
if (isJQuery && options?.clickType == 'jQuery') {
console.log(`Click (${selector}) with jQuery`);
await clickWithJQuery(el, selector, options, options?.timeout ? (options.timeout / 100) : 300);
} else {
console.log(`Click (${selector}) with Puppeteer`);
if (options?.text) {
await findByTextSelectorAndClick(el, selector, options.text);
} else {
await el.waitForSelector(selector, options);
await el.focus(selector);
await el.click(selector);
}

}
}
waitTillHTMLRendered-Funktion

Code: Select all

export async function waitTillHTMLRendered(page: Page | Frame, timeout = 60000) {
const checkDurationMsecs = 1000;
const maxChecks = timeout / checkDurationMsecs;
let lastHTMLSize = 0;
let checkCounts = 1;
let countStableSizeIterations = 0;
const minStableSizeIterations = 3;

while (checkCounts++  document.body.innerHTML.length);

// console.log('last: ', lastHTMLSize, '  curr: ', currentHTMLSize, " body html size: ", bodyHTMLSize);

if (lastHTMLSize != 0 && currentHTMLSize == lastHTMLSize)
countStableSizeIterations++;
else
countStableSizeIterations = 0; //reset the counter

if (countStableSizeIterations >= minStableSizeIterations) {
console.log('Page rendered fully..' + bodyHTMLSize);
break;
}

lastHTMLSize = currentHTMLSize;
} catch (error) {
// Execution context was destroyed (navigation occurred)
console.log('waitTillHTMLRendered: Context destroyed, waiting for page to stabilize...', error);
await new Promise(res => setTimeout(res, 6000));
// Reset counters after navigation
lastHTMLSize = 0;
countStableSizeIterations = 0;
}

await new Promise(res => setTimeout(res, checkDurationMsecs));
}
}
Ich habe Version 24 von Puppeter

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post