以下代码随机失败,并显示:
执行上下文被破坏,很可能是因为导航。
为什么? 有什么解决方法吗?
我使用木偶师版本 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/#',............