【发布时间】:2023-03-15 08:48:01
【问题描述】:
即使我使用 if/else 语句进行检查,我也会收到承诺拒绝。当我遍历 div (const items) 时,"description-main" 类在所有项目上。但是,"description-optional" 类并不总是存在。如何跳过它或将null 分配给它以继续循环而不给出任何错误?
// Imports: Dependencies
const puppeteer = require('puppeteer');
// Puppeteer: Generate CSV
(async () => {
try {
// Start Browser And Create New Page
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
// Go To Page
await page.goto('http://website.com', { waitUntil: 'networkidle2' });
const items = await page.$$('.item');
for (let i = 0; i <= items.length; i ++) {
const itemDescription = await items[i].$eval('.description-main', div => div.innerText.trim());
const itemDescription2 = null | await items[i].$eval('description-optional', div => div.innerText.trim());
console.log('Item: ' + itemDescription + itemDescription2);
}
}
catch (error) {
console.log(error);
}
})()
【问题讨论】:
标签: javascript node.js asynchronous promise puppeteer