from .base_handler import BaseHandler import logging import asyncio logger = logging.getLogger(__name__) class WBMHandler(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"[WBM] Opening listing overview page: {listing['link']}") await page.goto(listing["link"], wait_until="networkidle") logger.info("[WBM] Overview page loaded") 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/wbm_debug.html", "w", encoding="utf-8") as f: f.write(html_content) except Exception as e: logger.warning(f"[WBM] Could not save debug HTML: {e}") # 404/permanent fail detection error_texts = [ "Keine passenden Angebote gefunden", "Das Angebot existiert nicht mehr", "Die gewünschte Seite konnte nicht gefunden werden", "404", "Es wurden keine Immobilien gefunden" ] page_text = await page.text_content('body') if page_text: for err in error_texts: if err in page_text: result["permanent_fail"] = True result["message"] = "Listing is no longer available (404 detected on WBM)." logger.warning(f"[WBM] Permanent fail: {err}") await page.close() return result # Find and follow the 'Details' link to the detail page logger.info("[WBM] Looking for 'Details' link to open detail page...") detail_link = None detail_selectors = [ 'a.btn.sign[title="Details"]', 'a.immo-button-cta[title="Details"]', 'a[title="Details"]', ] for sel in detail_selectors: links = await page.query_selector_all(sel) logger.info(f"[WBM] Selector '{sel}' found {len(links)} matches for details link") for link in links: try: if await link.is_visible(): detail_link = link break except Exception as e: logger.warning(f"[WBM] Error checking details link visibility: {e}") if detail_link: break if not detail_link: result["message"] = "No details link found on overview page." await page.close() return result # Click the details link and wait for navigation logger.info("[WBM] Clicking details link to open detail page...") await detail_link.click() await page.wait_for_load_state("networkidle") await asyncio.sleep(2) # Save HTML of detail page for debugging try: html_content = await page.content() with open("data/wbm_detail_debug.html", "w", encoding="utf-8") as f: f.write(html_content) except Exception as e: logger.warning(f"[WBM] Could not save detail debug HTML: {e}") # Look for application button on detail page logger.info("[WBM] Looking for application button on detail page...") selectors = [ 'a[href*="expose-anfordern"]', 'a[href*="bewerben"]', 'a:has-text("Anfragen")', 'button:has-text("Interesse")', 'a:has-text("Bewerben")', 'button:has-text("Bewerben")', 'button.btn', ] apply_btn = None for sel in selectors: all_btns = await page.query_selector_all(sel) logger.info(f"[WBM] Selector '{sel}' found {len(all_btns)} matches on detail page") for btn in all_btns: try: if await btn.is_visible(): apply_btn = btn logger.info(f"[WBM] Found visible application button with selector '{sel}' on detail page") break except Exception as e: logger.warning(f"[WBM] Error checking button visibility: {e}") if apply_btn: break if apply_btn: logger.info("[WBM] Found application button, scrolling into view...") await apply_btn.scroll_into_view_if_needed() await asyncio.sleep(0.5) logger.info("[WBM] Clicking application button...") await apply_btn.click() await asyncio.sleep(2) result["success"] = True result["message"] = "Application button clicked on detail page. (Submission not implemented)" else: result["message"] = "No application button found on detail page." except Exception as e: result["message"] = f"Error during application: {e}" logger.error(f"[WBM] Application error: {e}") finally: await page.close() return result