Forráskód Böngészése

Added try catch block, fallback selector is used when original selector (body) is not found.

guillermoscript 2 éve
szülő
commit
0d547d9282
1 módosított fájl, 9 hozzáadás és 0 törlés
  1. 9 0
      src/main.ts

+ 9 - 0
src/main.ts

@@ -39,9 +39,18 @@ if (process.env.NO_CRAWL !== "true") {
       const title = await page.title();
       log.info(`Crawling ${request.loadedUrl}...`);
 
+      try {
         await page.waitForSelector(config.selector, {
           timeout: config.waitForSelectorTimeout ?? 1000,
         });
+      } catch (e) {
+        // If the selector is not found, let the user know
+        log.warning(`Selector "${config.selector}" not found on ${request.loadedUrl}, Falling back to "body"`);
+        // using body as a fallback
+        await page.waitForSelector("body", {
+          timeout: config.waitForSelectorTimeout ?? 1000,
+        });
+      }
 
       const html = await getPageHtml(page);