diff --git a/handlers/wbm_handler.py b/handlers/wbm_handler.py index 95ce015..7a4268a 100644 --- a/handlers/wbm_handler.py +++ b/handlers/wbm_handler.py @@ -1,6 +1,7 @@ from .base_handler import BaseHandler import logging import asyncio +import os from pathlib import Path logger = logging.getLogger(__name__) @@ -124,23 +125,119 @@ class WBMHandler(BaseHandler): break if apply_btn: - logger.info("[WBM] Found application button, scrolling into view...") + logger.info("[WBM] Found submit button, scrolling form into view...") await apply_btn.scroll_into_view_if_needed() - await asyncio.sleep(0.5) - logger.info("[WBM] Clicking application button...") + await asyncio.sleep(1) + + # Handle cookies/consent again after scrolling (modals may reappear) + await self.handle_cookies(page) + await self.handle_consent(page) + await asyncio.sleep(1) + + # Fill out the form fields before clicking submit + logger.info("[WBM] Filling out application form...") + form_filled = False + try: + # Anrede dropdown + anrede_select = await page.query_selector('select[name*="anrede" i]') + if anrede_select: + await anrede_select.select_option(os.getenv("FORM_ANREDE", "Frau")) + logger.debug("[WBM] Filled Anrede") + form_filled = True + + # Name (Nachname) + name_input = await page.query_selector('input[name*="name" i]:not([name*="vorname" i])') + if name_input: + await name_input.fill(os.getenv("FORM_NACHNAME", "")) + logger.debug("[WBM] Filled Name") + form_filled = True + + # Vorname + vorname_input = await page.query_selector('input[name*="vorname" i]') + if vorname_input: + await vorname_input.fill(os.getenv("FORM_VORNAME", "")) + logger.debug("[WBM] Filled Vorname") + form_filled = True + + # Email + email_input = await page.query_selector('input[name*="email" i]') + if email_input: + await email_input.fill(os.getenv("FORM_EMAIL", "")) + logger.debug("[WBM] Filled Email") + form_filled = True + + # Telefon + phone_input = await page.query_selector('input[name*="telefon" i]') + if phone_input: + await phone_input.fill(os.getenv("FORM_PHONE", "")) + logger.debug("[WBM] Filled Phone") + + # Strasse + strasse_input = await page.query_selector('input[name*="strasse" i]') + if strasse_input: + await strasse_input.fill(os.getenv("FORM_STRASSE", "")) + logger.debug("[WBM] Filled Strasse") + + # Hausnummer + hausnummer_input = await page.query_selector('input[name*="hausnummer" i]') + if hausnummer_input: + await hausnummer_input.fill(os.getenv("FORM_HAUSNUMMER", "")) + logger.debug("[WBM] Filled Hausnummer") + + # PLZ + plz_input = await page.query_selector('input[name*="plz" i]') + if plz_input: + await plz_input.fill(os.getenv("FORM_PLZ", "")) + logger.debug("[WBM] Filled PLZ") + + # Ort + ort_input = await page.query_selector('input[name*="ort" i]') + if ort_input: + await ort_input.fill(os.getenv("FORM_ORT", "")) + logger.debug("[WBM] Filled Ort") + + # Datenschutz checkbox + datenschutz_checkbox = await page.query_selector('input[name*="datenschutz" i][type="checkbox"]') + if datenschutz_checkbox: + is_checked = await datenschutz_checkbox.is_checked() + if not is_checked: + await datenschutz_checkbox.check() + logger.debug("[WBM] Checked Datenschutz") + + if not form_filled: + logger.error("[WBM] No form fields found - form may not be visible") + result["message"] = "Form not found or not visible" + await page.screenshot(path=DATA_DIR / f"wbm_no_form_{listing['id']}.png") + await page.close() + return result + + logger.info("[WBM] Form filled successfully") + await asyncio.sleep(1) + + # Save screenshot before submit + await page.screenshot(path=DATA_DIR / f"wbm_before_submit_{listing['id']}.png") + + except Exception as e: + logger.error(f"[WBM] Error filling form: {e}") + result["message"] = f"Error filling form: {e}" + await page.screenshot(path=DATA_DIR / f"wbm_form_error_{listing['id']}.png") + await page.close() + return result + + logger.info("[WBM] Clicking submit button...") await apply_btn.click() - await asyncio.sleep(2) + await asyncio.sleep(3) # --- Post-click confirmation logic --- - logger.info("[WBM] Clicked application button, checking for confirmation...") + logger.info("[WBM] Clicked submit button, checking for confirmation...") # Save screenshot and HTML after click try: - await page.screenshot(path="data/wbm_after_apply.png") + await page.screenshot(path=DATA_DIR / "wbm_after_apply.png") logger.info("[WBM] Saved screenshot after application click.") except Exception as e: logger.warning(f"[WBM] Could not save screenshot: {e}") try: html_after = await page.content() - with open("data/wbm_after_apply.html", "w", encoding="utf-8") as f: + with open(DATA_DIR / "wbm_after_apply.html", "w", encoding="utf-8") as f: f.write(html_after) logger.info("[WBM] Saved HTML after application click.") except Exception as e: