在运行If语句时,如果页面元素不存在,页面加载将失败并产生正确的错误消息,但是当该元素存在时,两种情况都将运行,成功代码将进行操作以及失败将进行操作。
尝试了await页面。waitForSelector和await页面。$(selector)都无效,并且
await page.waitForSelector('#example').then(() => {
console.log('SUCCESS');
}).catch(e => {
console.log('FAIL');
});
if (await page.$('.SELECTOR') !== null)
console.log(success(await page.title() + ' SUCCESS LOAD 1 - URL Page Load')); // console log
// on success continue below
else
console.log(error(await page.title() + ' FAIL LOAD 1 - URL Page Load')); // console log
await page.screenshot({path: './screenshots/Fail_Load_1_PageDidNotLoad.jpeg'}); // screenshot
await page.evaluate(() => alert('FAIL LOAD 1 - URL Page Did Not Load')); // pop up alert
await browser.close(); // release memory
return process.exit(1); // exit process
如何正确合并THEN。
答案 0 :(得分:0)
已解决,如果有人遇到同样的挑战。
try {
await page.waitForSelector('.smallText', { timeout: 5000 })
console.log(success(await page.title() + ' SUCCESS LOAD 1 - URL Page Load')); // console log
await page.screenshot({path: './screenshots/Success_Load_1_PageLoad.jpeg'}); // screenshot
} catch (err) {
console.log(error(await page.title() + ' FAIL LOAD 1 - URL Page Load')); // console log
await page.screenshot({path: './screenshots/Fail_Load_1_PageDidNotLoad.jpeg'}); // screenshot
await page.evaluate(() => alert('FAIL LOAD 1 - URL Page Did Not Load')); // pop up alert
await browser.close(); // release memory
return process.exit(1); // exit process
//return false
}