diff --git a/README.md b/README.md index 63c2006..ac0de02 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ The auto-apply feature is experimental and only works for some housing companies | Company | Status | Notes | |---------|--------|-------| | HOWOGE | Working | Tested and functional | -| Degewo | Experimental | Uses Wohnungshelden portal, may need maintenance | +| Degewo | Experimental | Uses Wohnungshelden portal | +| Stadt und Land | Experimental | Uses Wohnungshelden portal | | Gewobag | Not working | Needs implementation | | Gesobau | Not working | Needs implementation | -| Stadt und Land | Not working | Needs implementation | | WBM | Not working | Needs implementation | | WGcompany | Not supported | Monitoring only, no auto-apply | diff --git a/monitor.py b/monitor.py index cd8dc4f..fadc3a6 100644 --- a/monitor.py +++ b/monitor.py @@ -850,6 +850,11 @@ class ApplicationHandler: return result async def _apply_stadtundland(self, listing: dict, result: dict) -> dict: + """ + Stadt und Land uses Wohnungshelden (app.wohnungshelden.de) for their application system. + The application form is loaded in an iframe from a different domain. + We need to navigate directly to the iframe URL. + """ page = await self.context.new_page() try: logger.info(f"[STADTUNDLAND] Opening page: {listing['link']}") @@ -857,6 +862,7 @@ class ApplicationHandler: logger.info("[STADTUNDLAND] Page loaded") await asyncio.sleep(2) + # Dismiss cookie banner try: cookie_btn = await page.query_selector('button:has-text("Akzeptieren"), button:has-text("Alle akzeptieren")') if cookie_btn and await cookie_btn.is_visible(): @@ -865,141 +871,183 @@ class ApplicationHandler: await asyncio.sleep(1) except: pass - # Stadt und Land has the contact form directly on the page - logger.info("[STADTUNDLAND] Looking for contact form fields...") + # Look for "Kontakt" or "Anfragen" button to open the form + logger.info("[STADTUNDLAND] Looking for contact button...") + apply_btn = await page.query_selector('a:has-text("Kontakt"), button:has-text("Kontakt"), a:has-text("Anfragen"), button:has-text("Anfragen"), a:has-text("kontaktieren"), button:has-text("kontaktieren")') + if apply_btn and await apply_btn.is_visible(): + logger.info("[STADTUNDLAND] Found contact button, clicking...") + await apply_btn.click() + await asyncio.sleep(3) - form_filled = False + # Stadt und Land uses Wohnungshelden iframe for the application form + iframe_element = await page.query_selector('iframe[src*="wohnungshelden.de"]') + if iframe_element: + iframe_url = await iframe_element.get_attribute('src') + logger.info(f"[STADTUNDLAND] Found Wohnungshelden iframe: {iframe_url}") - # Fill Vorname - try: - vorname_field = await page.query_selector('input[name*="vorname" i], input[placeholder*="Vorname" i], input#vorname') - if vorname_field: - await vorname_field.fill(FORM_VORNAME) - logger.info(f"[STADTUNDLAND] Filled Vorname: {FORM_VORNAME}") - form_filled = True - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Vorname: {e}") - - # Fill Nachname - try: - nachname_field = await page.query_selector('input[name*="nachname" i], input[placeholder*="Nachname" i], input#nachname') - if nachname_field: - await nachname_field.fill(FORM_NACHNAME) - logger.info(f"[STADTUNDLAND] Filled Nachname: {FORM_NACHNAME}") - form_filled = True - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Nachname: {e}") - - # Fill Telefonnummer - try: - tel_field = await page.query_selector('input[name*="telefon" i], input[type="tel"], input[placeholder*="Telefon" i]') - if tel_field: - await tel_field.fill(FORM_PHONE) - logger.info(f"[STADTUNDLAND] Filled Telefon: {FORM_PHONE}") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Telefon: {e}") - - # Fill E-Mail - try: - email_field = await page.query_selector('input[type="email"], input[name*="email" i], input[name*="mail" i]') - if email_field: - await email_field.fill(FORM_EMAIL) - logger.info(f"[STADTUNDLAND] Filled E-Mail: {FORM_EMAIL}") - form_filled = True - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill E-Mail: {e}") - - # Fill Straße (street) - try: - strasse_field = await page.query_selector('input[name*="strasse" i], input[name*="straße" i], input[placeholder*="Straße" i], input#strasse') - if strasse_field and FORM_STRASSE: - await strasse_field.fill(FORM_STRASSE) - logger.info(f"[STADTUNDLAND] Filled Straße: {FORM_STRASSE}") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Straße: {e}") - - # Fill Hausnummer - try: - hausnummer_field = await page.query_selector('input[name*="hausnummer" i], input[name*="hausnr" i], input[placeholder*="Hausnummer" i], input#hausnummer') - if hausnummer_field and FORM_HAUSNUMMER: - await hausnummer_field.fill(FORM_HAUSNUMMER) - logger.info(f"[STADTUNDLAND] Filled Hausnummer: {FORM_HAUSNUMMER}") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Hausnummer: {e}") - - # Fill PLZ - try: - plz_field = await page.query_selector('input[name*="plz" i], input[placeholder*="PLZ" i], input#plz') - if plz_field and FORM_PLZ: - await plz_field.fill(FORM_PLZ) - logger.info(f"[STADTUNDLAND] Filled PLZ: {FORM_PLZ}") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill PLZ: {e}") - - # Fill Ort (city) - try: - ort_field = await page.query_selector('input[name*="ort" i], input[placeholder*="Ort" i], input#ort') - if ort_field and FORM_ORT: - await ort_field.fill(FORM_ORT) - logger.info(f"[STADTUNDLAND] Filled Ort: {FORM_ORT}") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not fill Ort: {e}") - - # Check Datenschutz checkbox - try: - datenschutz_checkbox = await page.query_selector('input[type="checkbox"][name*="datenschutz" i], input[type="checkbox"][name*="privacy" i]') - if datenschutz_checkbox and not await datenschutz_checkbox.is_checked(): - await datenschutz_checkbox.click() - logger.info("[STADTUNDLAND] Checked Datenschutz checkbox") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not check Datenschutz: {e}") - - # Check Provision checkbox - try: - provision_checkbox = await page.query_selector('input[type="checkbox"][name*="provision" i]') - if provision_checkbox and not await provision_checkbox.is_checked(): - await provision_checkbox.click() - logger.info("[STADTUNDLAND] Checked Provision checkbox") - except Exception as e: - logger.warning(f"[STADTUNDLAND] Could not check Provision: {e}") - - await asyncio.sleep(1) - - # Screenshot before submitting - screenshot_path = DATA_DIR / f"stadtundland_form_{listing['id']}.png" - await page.screenshot(path=str(screenshot_path), full_page=True) - logger.info(f"[STADTUNDLAND] Saved form screenshot to {screenshot_path}") - - if form_filled: - # Submit the form - look for submit button + # Navigate to the iframe URL directly in a new page for full access + iframe_page = await self.context.new_page() try: - submit_btn = await page.query_selector('button[type="submit"], input[type="submit"], button:has-text("prüfen"), button:has-text("Absenden"), button:has-text("Senden")') - if submit_btn and await submit_btn.is_visible(): - await submit_btn.click() - logger.info("[STADTUNDLAND] Clicked submit button") - await asyncio.sleep(3) + await iframe_page.goto(iframe_url, wait_until="networkidle") + await asyncio.sleep(2) + logger.info("[STADTUNDLAND] Loaded Wohnungshelden application page") - # Screenshot after submission - screenshot_path = DATA_DIR / f"stadtundland_submitted_{listing['id']}.png" - await page.screenshot(path=str(screenshot_path), full_page=True) - logger.info(f"[STADTUNDLAND] Saved submission screenshot to {screenshot_path}") + # Take screenshot of the Wohnungshelden form + screenshot_path = DATA_DIR / f"stadtundland_wohnungshelden_{listing['id']}.png" + await iframe_page.screenshot(path=str(screenshot_path), full_page=True) + logger.info(f"[STADTUNDLAND] Saved Wohnungshelden screenshot to {screenshot_path}") - result["success"] = True - result["message"] = "Application submitted" + # Save HTML for debugging + html_content = await iframe_page.content() + html_path = DATA_DIR / f"stadtundland_wohnungshelden_{listing['id']}.html" + with open(html_path, 'w', encoding='utf-8') as f: + f.write(html_content) + logger.info(f"[STADTUNDLAND] Saved HTML to {html_path}") + + # Fill out Wohnungshelden form (same as Degewo) + form_filled = False + + # Anrede (Salutation) - ng-select dropdown + try: + salutation_dropdown = await iframe_page.query_selector('#salutation-dropdown, ng-select[id*="salutation"]') + if salutation_dropdown: + await salutation_dropdown.click() + await asyncio.sleep(0.5) + anrede_option = await iframe_page.query_selector(f'.ng-option:has-text("{FORM_ANREDE}")') + if anrede_option: + await anrede_option.click() + logger.info(f"[STADTUNDLAND] Selected Anrede: {FORM_ANREDE}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not set Anrede: {e}") + + # Vorname (First name) + try: + vorname_field = await iframe_page.query_selector('#firstName') + if vorname_field: + await vorname_field.fill(FORM_VORNAME) + logger.info(f"[STADTUNDLAND] Filled Vorname: {FORM_VORNAME}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not fill Vorname: {e}") + + # Nachname (Last name) + try: + nachname_field = await iframe_page.query_selector('#lastName') + if nachname_field: + await nachname_field.fill(FORM_NACHNAME) + logger.info(f"[STADTUNDLAND] Filled Nachname: {FORM_NACHNAME}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not fill Nachname: {e}") + + # E-Mail + try: + email_field = await iframe_page.query_selector('#email') + if email_field: + await email_field.fill(FORM_EMAIL) + logger.info(f"[STADTUNDLAND] Filled E-Mail: {FORM_EMAIL}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not fill E-Mail: {e}") + + # Telefonnummer + try: + tel_field = await iframe_page.query_selector('input[id*="telefonnummer"]') + if tel_field: + await tel_field.fill(FORM_PHONE) + logger.info(f"[STADTUNDLAND] Filled Telefon: {FORM_PHONE}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not fill Telefon: {e}") + + # Anzahl einziehende Personen + try: + personen_field = await iframe_page.query_selector('input[id*="numberPersonsTotal"]') + if personen_field: + await personen_field.fill(FORM_PERSONS) + logger.info(f"[STADTUNDLAND] Filled Anzahl Personen: {FORM_PERSONS}") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not fill Anzahl Personen: {e}") + + # "Für sich selbst" dropdown + try: + selbst_dropdown = await iframe_page.query_selector('ng-select[id*="fuer_wen"]') + if selbst_dropdown: + await selbst_dropdown.click() + await asyncio.sleep(0.5) + selbst_option = await iframe_page.query_selector('.ng-option:has-text("Für mich selbst"), .ng-option:has-text("selbst")') + if selbst_option: + await selbst_option.click() + logger.info("[STADTUNDLAND] Selected: Für mich selbst") + form_filled = True + except Exception as e: + logger.warning(f"[STADTUNDLAND] Could not set 'Für sich selbst': {e}") + + await asyncio.sleep(1) + + # Take screenshot after filling form + screenshot_path = DATA_DIR / f"stadtundland_form_filled_{listing['id']}.png" + await iframe_page.screenshot(path=str(screenshot_path), full_page=True) + logger.info(f"[STADTUNDLAND] Saved filled form screenshot to {screenshot_path}") + + # Try to submit + if form_filled: + try: + submit_selectors = [ + 'button[type="submit"]', + 'input[type="submit"]', + 'button:has-text("Absenden")', + 'button:has-text("Senden")', + 'button:has-text("Anfrage")', + '.btn-primary', + ] + + submit_btn = None + for selector in submit_selectors: + submit_btn = await iframe_page.query_selector(selector) + if submit_btn and await submit_btn.is_visible(): + logger.info(f"[STADTUNDLAND] Found submit button with selector: {selector}") + break + submit_btn = None + + if submit_btn: + await submit_btn.click() + logger.info("[STADTUNDLAND] Clicked submit button") + await asyncio.sleep(3) + + # Take screenshot after submission + screenshot_path = DATA_DIR / f"stadtundland_submitted_{listing['id']}.png" + await iframe_page.screenshot(path=str(screenshot_path), full_page=True) + logger.info(f"[STADTUNDLAND] Saved submission screenshot to {screenshot_path}") + + result["success"] = True + result["message"] = "Application submitted via Wohnungshelden" + else: + result["success"] = False + result["message"] = "Form filled, submit button not found" + logger.warning("[STADTUNDLAND] Submit button not found") + except Exception as e: + result["success"] = False + result["message"] = f"Form filled, submit error: {str(e)}" + logger.warning(f"[STADTUNDLAND] Submit error: {e}") else: - result["success"] = True - result["message"] = "Form filled, submit button not found" - logger.warning("[STADTUNDLAND] Submit button not found") - except Exception as e: - result["success"] = True - result["message"] = f"Form filled, submit error: {str(e)}" - logger.warning(f"[STADTUNDLAND] Submit error: {e}") + result["success"] = False + result["message"] = "No form fields found in Wohnungshelden" + logger.warning("[STADTUNDLAND] Could not find form fields in Wohnungshelden") + finally: + await iframe_page.close() else: - result["message"] = "No form fields found" - logger.warning("[STADTUNDLAND] Could not find form fields") + # No Wohnungshelden iframe found + result["success"] = False + result["message"] = "No Wohnungshelden iframe found" + logger.warning("[STADTUNDLAND] No Wohnungshelden iframe found") screenshot_path = DATA_DIR / f"stadtundland_nobtn_{listing['id']}.png" - await page.screenshot(path=str(screenshot_path)) + await page.screenshot(path=str(screenshot_path), full_page=True) + except Exception as e: + result["success"] = False result["message"] = f"Error: {str(e)}" logger.error(f"[STADTUNDLAND] Exception: {str(e)}") finally: