diff --git a/robot.js b/robot.js index b6b8bf8..e6db677 100644 --- a/robot.js +++ b/robot.js @@ -281,7 +281,7 @@ async function runMultiasistencia(ownerId, user, pass, gremiosDB) { } // ========================================== -// 🧹 HOMESERVE (ROBOT MEJORADO Y DETECTOR DE URGENCIAS) +// 🧹 HOMESERVE (MODO DEBUG ACTIVADO + SCRAPING MANTENIDO) // ========================================== async function runHomeserve(ownerId, user, pass, gremiosDB) { const browser = await chromium.launch({ @@ -295,20 +295,58 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) { }); const page = await browser.newPage(); try { - console.log("🌍 [HomeServe] Entrando..."); + console.log("🌍 [HomeServe] Iniciando navegación a la página de login..."); await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=PROF_PASS'); - if (await page.isVisible('input[name="CODIGO"]')) { + // 1. COMPROBAR SI ESTAMOS EN EL LOGIN + const tieneUser = await page.isVisible('input[name="CODIGO"]'); + const tienePass = await page.isVisible('input[type="password"]'); + + if (tieneUser && tienePass) { + console.log(`🔑 [HomeServe] Formulario detectado. Intentando login para usuario: ${user}...`); await page.fill('input[name="CODIGO"]', user); await page.fill('input[type="password"]', pass); + + console.log("⌨️ [HomeServe] Pulsando Enter..."); await page.keyboard.press('Enter'); - await page.waitForTimeout(5000); + + // Esperamos a que la URL cambie o cargue algo + await page.waitForTimeout(6000); + + // 2. VERIFICAR SI EL LOGIN FALLÓ + const errorMsg = await page.evaluate(() => { + // Buscamos textos comunes de error en HomeServe + const body = document.body.innerText.toUpperCase(); + if (body.includes("ERROR") || body.includes("INCORRECTA") || body.includes("DENEGADO")) { + return body.substring(0, 100); // Nos devuelve el principio del error + } + return null; + }); + + if (errorMsg) { + console.error(`❌ [HomeServe] ¡ERROR DE AUTENTICACIÓN!: ${errorMsg}`); + } else { + console.log(`✅ [HomeServe] Parece que hemos entrado. URL actual: ${page.url()}`); + } + + } else { + console.log("⚠️ [HomeServe] No se ve el formulario de login. ¿Ya estábamos dentro o ha cambiado la web?"); } + // 3. INTENTAR IR A LA LISTA + console.log("📋 [HomeServe] Yendo a la lista de servicios..."); await gotoWithRetry(page, 'https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=lista_servicios_total'); - await page.waitForTimeout(3000); + await page.waitForTimeout(4000); + + // 4. DEBUG DE LA TABLA + const debugTabla = await page.evaluate(() => { + const tabla = document.querySelector('table[bgcolor="#FCF4D6"]'); + if (!tabla) return "NO ENCONTRADA"; + const filas = tabla.querySelectorAll('tr').length; + return `ENCONTRADA con ${filas} filas`; + }); + console.log(`📊 [HomeServe] Estado de la tabla: ${debugTabla}`); - // 🛑 NUEVO: Ahora extraemos la referencia Y LOS ICONOS de la misma pasada const listaConIconos = await page.evaluate(() => { const results = []; const rows = Array.from(document.querySelectorAll('table[bgcolor="#FCF4D6"] tr')); @@ -335,10 +373,11 @@ async function runHomeserve(ownerId, user, pass, gremiosDB) { if (refs.length > 0) { await syncAndArchive(ownerId, 'homeserve', refs); + console.log(`✅ [HomeServe] ${refs.length} expedientes detectados con éxito.`); + } else { + console.log("❓ [HomeServe] Login aparentemente OK pero 0 expedientes en la tabla."); } - console.log(`🔍 [HomeServe] ${refs.length} expedientes detectados.`); - for (const ref of refs) { await gotoWithRetry(page, `https://www.clientes.homeserve.es/cgi-bin/fccgi.exe?w3exec=ver_servicioencurso&Servicio=${ref}`); await page.waitForTimeout(2000);