Puppeteer 未处理对 waitForXPath 的拒绝
Puppeteer unhandled rejection on waitForXPath
我在使用 waitForXpath
等待并不总是显示的元素时遇到问题。
const isElementShown = await page
.waitForXPath('/html/body/div[4]/div[2]/iframe', {
visible: true,
timeout: 12000,
})
.then(result => {
console.log(`element shown: ${result}`);
return result;
})
.catch(exception => console.log(`element not shown: ${exception}`));
if (isElementShown) {
// do something
}
即使我有 .catch()
,该函数也会停止执行并在此处捕获异常:
process.on('unhandledRejection', (reason: { stack: unknown }, promise) => {
try {
console.log(`SYSTEM WIDE UNHANDLED REJECTION: ${reason.stack}`);
return reportError_async(`${reason.stack}`);
} catch (exception) {
console.log('exception', exception);
}
return null;
});
异常信息为:
Error: target should exist before targetInfoChanged
使用Puppeteer v9.1.0
如果你需要这一切只是因为你有这个条件:
if (isElementShown) {
// do something
}
为什么不使用 page.$()
?
const isElementShown = await page.$('html > body > div:nth-child(4) > div:nth-child(2) > iframe');
if (isElementShown) {
// do something
}
如果找不到元素,它不会抛出任何异常。然而,你不能在这里使用 xPath,只有 CSS 个选择器被征用 here,但那应该不是那么麻烦。
看起来这是 9.1.0
版本的 Puppeteer 的问题。版本 9.1.1
解决了这个问题。
相关 Github 问题:https://github.com/puppeteer/puppeteer/pull/7203
我在使用 waitForXpath
等待并不总是显示的元素时遇到问题。
const isElementShown = await page
.waitForXPath('/html/body/div[4]/div[2]/iframe', {
visible: true,
timeout: 12000,
})
.then(result => {
console.log(`element shown: ${result}`);
return result;
})
.catch(exception => console.log(`element not shown: ${exception}`));
if (isElementShown) {
// do something
}
即使我有 .catch()
,该函数也会停止执行并在此处捕获异常:
process.on('unhandledRejection', (reason: { stack: unknown }, promise) => {
try {
console.log(`SYSTEM WIDE UNHANDLED REJECTION: ${reason.stack}`);
return reportError_async(`${reason.stack}`);
} catch (exception) {
console.log('exception', exception);
}
return null;
});
异常信息为:
Error: target should exist before targetInfoChanged
使用Puppeteer v9.1.0
如果你需要这一切只是因为你有这个条件:
if (isElementShown) {
// do something
}
为什么不使用 page.$()
?
const isElementShown = await page.$('html > body > div:nth-child(4) > div:nth-child(2) > iframe');
if (isElementShown) {
// do something
}
如果找不到元素,它不会抛出任何异常。然而,你不能在这里使用 xPath,只有 CSS 个选择器被征用 here,但那应该不是那么麻烦。
看起来这是 9.1.0
版本的 Puppeteer 的问题。版本 9.1.1
解决了这个问题。
相关 Github 问题:https://github.com/puppeteer/puppeteer/pull/7203