wohnbot/handlers/wbm_handler.py

59 lines
2.2 KiB
Python
Raw Normal View History

2025-12-27 11:59:04 +01:00
from .base_handler import BaseHandler
import logging
import asyncio
logger = logging.getLogger(__name__)
class WBMHandler(BaseHandler):
async def apply(self, listing: dict, result: dict) -> dict:
page = await self.context.new_page()
try:
logger.info(f"[WBM] Opening page: {listing['link']}")
await page.goto(listing["link"], wait_until="networkidle")
logger.info("[WBM] Page loaded")
await asyncio.sleep(2)
# Handle cookies and consent
await self.handle_cookies(page)
await self.handle_consent(page)
# Look for application button
logger.info("[WBM] Looking for application button...")
selectors = [
'a[href*="bewerben"]',
'button:has-text("Bewerben")'
]
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")
for btn in all_btns:
try:
if await btn.is_visible():
apply_btn = btn
logger.info(f"[WBM] Found visible button with selector '{sel}'")
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 button...")
await apply_btn.click()
await asyncio.sleep(2)
result["success"] = True
result["message"] = "Application submitted successfully."
else:
result["message"] = "No application button found."
except Exception as e:
result["message"] = f"Error during application: {e}"
logger.error(f"[WBM] Application error: {e}")
finally:
await page.close()
return result