from .base_handler import BaseHandler import logging import asyncio from pathlib import Path logger = logging.getLogger(__name__) DATA_DIR = Path("data/wbm") DATA_DIR.mkdir(parents=True, exist_ok=True) 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_DIR / "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["deactivated"] = True result["message"] = "Listing is no longer available (404 detected on WBM)." logger.warning(f"[WBM] Permanent fail: {err}") await page.close() await page.close() return result # Check if we're already on the detail page (URL contains '/details/') current_url = page.url if '/details/' not in current_url: # 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) else: logger.info("[WBM] Already on detail page, skipping details link search") # 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 = [ 'button.btn.btn-primary[type="submit"]', # The actual form submit button 'button:has-text("Anfrage absenden")', '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) # --- Post-click confirmation logic --- logger.info("[WBM] Clicked application button, checking for confirmation...") # Save screenshot and HTML after click try: await page.screenshot(path="data/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: f.write(html_after) logger.info("[WBM] Saved HTML after application click.") except Exception as e: logger.warning(f"[WBM] Could not save HTML after apply: {e}") # Look for confirmation message on the page confirmation_selectors = [ 'text="Vielen Dank"', 'text="Ihre Anfrage wurde gesendet"', 'text="Bestätigung"', 'div:has-text("Vielen Dank")', 'div:has-text("Ihre Anfrage wurde gesendet")', ] confirmed = False for sel in confirmation_selectors: try: el = await page.query_selector(sel) if el and await el.is_visible(): logger.info(f"[WBM] Found confirmation element: {sel}") confirmed = True break except Exception as e: logger.debug(f"[WBM] Error checking confirmation selector {sel}: {e}") if confirmed: result["success"] = True result["message"] = "Application submitted and confirmation detected." else: logger.warning("[WBM] No confirmation message detected after application click.") result["success"] = False result["message"] = "Clicked application button, but no confirmation detected. Check screenshot and HTML." 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