from .base_handler import BaseHandler import logging import asyncio logger = logging.getLogger(__name__) class GesobauHandler(BaseHandler): def __init__(self, browser_context): self.context = browser_context async def apply(self, listing: dict, result: dict) -> dict: page = await self.context.new_page() try: logger.info(f"[GESOBAU] Open: {listing['link']}") await page.goto(listing["link"], wait_until="networkidle") await asyncio.sleep(2) # Always handle cookies and consent before anything else await self.handle_cookies(page) await self.handle_consent(page) # Save HTML after modal handling for debugging try: html_content = await page.content() with open("data/gesobau_debug.html", "w", encoding="utf-8") as f: f.write(html_content) except Exception as e: logger.debug(f"[GESOBAU] Debug HTML not saved: {e}") # Tailored 404 detection: Angebot nicht mehr verfügbar if "Angebot nicht mehr verfügbar" in html_content: logger.warning("[GESOBAU] Permanent fail: Angebot nicht mehr verfügbar") result["permanent_fail"] = True result["message"] = "Listing is no longer available (Angebot nicht mehr verfügbar). Marked as permanent fail." return result # Look for application button logger.info("[GESOBAU] Searching for application button...") selectors = [ 'a[href*="bewerben"]', 'button:has-text("Bewerben")', 'a:has-text("Bewerben")', 'button.btn', ] apply_btn = None for sel in selectors: all_btns = await page.query_selector_all(sel) logger.debug(f"[GESOBAU] Selector '{sel}': {len(all_btns)} matches") for btn in all_btns: try: if await btn.is_visible(): apply_btn = btn logger.info(f"[GESOBAU] Found visible application button: {sel}") break except Exception as e: logger.debug(f"[GESOBAU] Button visibility error: {e}") if apply_btn: break if apply_btn: await apply_btn.scroll_into_view_if_needed() await asyncio.sleep(0.5) await apply_btn.click() await asyncio.sleep(2) result["success"] = True result["message"] = "Application submitted successfully." else: logger.warning("[GESOBAU] No application button found.") result["message"] = "No application button found." except Exception as e: result["message"] = f"Error during application: {e}" logger.error(f"[GESOBAU] Application error: {e}") finally: await page.close() return result