From 067cf9a7cbbacfb7ae250777920c2207a6864354 Mon Sep 17 00:00:00 2001 From: Deep Koluguri Date: Sun, 28 Jun 2026 21:41:25 -0400 Subject: [PATCH] Robust data extraction with fallback --- utility-agent/scraper.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/utility-agent/scraper.js b/utility-agent/scraper.js index 4bc7411..5fd4c2e 100644 --- a/utility-agent/scraper.js +++ b/utility-agent/scraper.js @@ -61,10 +61,14 @@ async function runScrapers(credentials) { ]); console.log('[Scraper] Logged into FirstEnergy. Extracting usage...'); - await page.waitForSelector('.usage-summary, .kwh-value', { timeout: 10000 }); - const usage = await page.$eval('.kwh-value', el => el.innerText); + await page.waitForSelector('.kwh, .usage-summary, .kwh-value', { timeout: 10000 }); + const usage = await page.evaluate(() => { + const el = document.querySelector('.kwh, .usage-summary, .kwh-value'); + if (!el) return 'N/A'; + return (el.getAttribute('data-chart-value') || el.innerText).trim(); + }); const cost = await page.$eval('.cost-value', el => el.innerText).catch(() => 'N/A'); - report += `💡 *FirstEnergy*: ${usage.trim()} kWh used today (~${cost.trim()})\n`; + report += `💡 *FirstEnergy*: ${usage} kWh used today (~${cost.trim()})\n`; } catch (err) { console.warn('[Scraper] FirstEnergy scraping failed:', err.message); report += `💡 *FirstEnergy*: ❌ Error scraping data (${err.message})\n`; @@ -81,17 +85,27 @@ async function runScrapers(credentials) { await page.goto('https://peopleseaccount.com/Portal/default.aspx', { waitUntil: 'domcontentloaded', timeout: 60000 }); - await page.waitForSelector('#txtLogin, input[name="txtLogin"]', { timeout: 30000 }); - await page.type('#txtLogin, input[name="txtLogin"]', credentials.peoplesGas.username); - await page.type('#txtpwd, input[name="txtpwd"]', credentials.peoplesGas.password); + await page.waitForSelector('#txtLogin', { timeout: 30000 }); + + // Clear fields first and type cleanly + await page.evaluate(() => { + document.querySelector('#txtLogin').value = ''; + document.querySelector('#txtpwd').value = ''; + }); + + await page.type('#txtLogin', credentials.peoplesGas.username); + await page.type('#txtpwd', credentials.peoplesGas.password); await page.click('#btnlogin'); console.log('[Scraper] Logged into Peoples Gas. Extracting usage...'); - await page.waitForSelector('.ccf-usage', { timeout: 10000 }); - const usage = await page.$eval('.ccf-usage', el => el.innerText); - const cost = await page.$eval('.ccf-cost', el => el.innerText).catch(() => 'N/A'); - report += `🔥 *Peoples Gas*: ${usage.trim()} CCF used today (~${cost.trim()})\n`; + await page.waitForSelector('.ccf-usage', { timeout: 15000 }).catch(() => null); + const usage = await page.evaluate(() => { + const el = document.querySelector('.ccf-usage, #aGasUsageCCF'); + return el ? el.innerText.replace('View in', '').trim() : 'N/A'; + }); + const cost = await page.$eval('.cost-value', el => el.innerText).catch(() => 'N/A'); + report += `🔥 *Peoples Gas*: ${usage} CCF used today (~${cost.trim()})\n`; } catch (err) { console.warn('[Scraper] Peoples Gas scraping failed:', err.message); report += `🔥 *Peoples Gas*: ❌ Error scraping data (${err.message})\n`;