gpt4 book ai didi

node.js - 伪造者page.evaluate()随机失败,出现: Execution context was destroyed,的原因很可能是由于导航

转载 作者:行者123 更新时间:2023-12-01 03:06:04 26 4
gpt4 key购买 nike

以下代码随机失败:

Execution context was destroyed, most likely because of navigation.



这是为什么?
任何解决方法?

我使用puppeteer版本1.19.0

注意:我正在寻找一种通用的解决方案,该解决方案既可以导航到具有重定向的页面,也可以导航到没有重定向的页面。

const puppeteer = require("puppeteer");

(async () => {
const browser = await puppeteer.launch();

try {
const page = await browser.newPage();

await page.setCacheEnabled(false);

const response = await page.goto("https://docs.cypress.io/", {
waitUntil: "networkidle0",
timeout: 60000
});

const pageUrls = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll("a"));

return links.map(link => link.href);
});

console.log({ pageUrls });
} catch (error) {
console.log(error.message);
}

await browser.close();
})();


最佳答案

解决方法-添加以下代码
await page.waitForNavigation()
调用page.goto()之后,或者如果page.click()发生相同问题,则可以等待使用上述方法进行导航。

const puppeteer = require("puppeteer");

(async () => {
const browser = await puppeteer.launch();

try {
const page = await browser.newPage();

await page.setCacheEnabled(false);

const response = await page.goto("https://docs.cypress.io/", {
waitUntil: "networkidle0",
timeout: 60000
});

await page.waitForNavigation();

const pageUrls = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll("a"));

return links.map(link => link.href);
});

console.log({ pageUrls });
} catch (error) {
console.log(error.message);
}

await browser.close();
})();

输出:
{ pageUrls:
[ 'https://twitter.com/amirrustam',
'https://www.componentsconf.com.au/workshops',
'https://www.cypress.io/',
'https://docs.cypress.io/guides/overview/why-cypress.html',
'https://docs.cypress.io/api/api/table-of-contents.html',
'https://docs.cypress.io/plugins/',
'https://docs.cypress.io/examples/examples/recipes.html',
'https://docs.cypress.io/faq/questions/using-cypress-faq.html',.................]

编辑
const puppeteer = require("puppeteer");

(async () => {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.setCacheEnabled(false);

await Promise.all([
page.waitForNavigation({ timeout: 60000 }),
page.goto("https://www.google.com/", {
waitUntil: "networkidle0",
timeout: 60000
})
])

const pageUrls = await page.evaluate(() => {
const links = Array.from(document.querySelectorAll("a"));
return links.map(link => link.href);
});
console.log({ pageUrls });
} catch (error) {
console.log(error.message);
}
await browser.close();
})();

输出:
{ pageUrls:
[ 'https://mail.google.com/mail/?tab=wm&ogbl',
'https://www.google.co.in/imghp?hl=en&tab=wi&ogbl',
'https://www.google.co.in/intl/en/about/products?tab=wh',
'https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=https://www.google.com/',
'https://www.google.com/#',............

关于node.js - 伪造者page.evaluate()随机失败,出现: Execution context was destroyed,的原因很可能是由于导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57583427/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com