【问题标题】:With If statement Page load error works, but page load success does not使用 If 语句页面加载错误有效,但页面加载成功无效
【发布时间】:2019-06-01 03:38:41
【问题描述】:

运行 If 语句时,如果页面元素不存在,页面加载失败并产生正确的错误消息,但当元素存在时,两个条件都会运行,成功代码操作以及失败操作。

试过 await page.waitForSelector 和 await page.$(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。

【问题讨论】:

  • 代码非常适合 FAIL 错误,但不适合 SUCCESS。当 SUCCESS FAIL 和 SUCCESS 都运行时。

标签: if-statement puppeteer


【解决方案1】:

已解决,如果有人有同样的挑战。

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 2012-08-11
    • 2012-09-01
    相关资源
    最近更新 更多