From 2f93e0dfdad785e111facc555788b069ff2b60a6 Mon Sep 17 00:00:00 2001 From: Jens Bergmann Date: Fri, 17 Jan 2025 22:18:50 +0100 Subject: [PATCH] fix: correct city selection when multiple cities are available for a ZIP code When multiple cities are available for a ZIP code, the bot now correctly selects the city specified in the YAML file's location field instead of always choosing the first option. The change: - Adds logic to select the correct city from dropdown based on location field - Adds a small delay after ZIP code input to allow dropdown to populate - Uses proper WebScrapingMixin method to read dropdown options --- src/kleinanzeigen_bot/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/kleinanzeigen_bot/__init__.py b/src/kleinanzeigen_bot/__init__.py index 390271f..372adcc 100644 --- a/src/kleinanzeigen_bot/__init__.py +++ b/src/kleinanzeigen_bot/__init__.py @@ -668,6 +668,18 @@ class KleinanzeigenBot(WebScrapingMixin): ############################# if ad_cfg["contact"]["zipcode"]: await self.web_input(By.ID, "pstad-zip", ad_cfg["contact"]["zipcode"]) + # Set city if location is specified + if ad_cfg["contact"].get("location"): + try: + await self.web_sleep(1) # Wait for city dropdown to populate + options = await self.web_find_all(By.CSS_SELECTOR, "#pstad-citychsr option") + for option in options: + option_text = await self.web_text(By.CSS_SELECTOR, "option", parent=option) + if option_text == ad_cfg["contact"]["location"]: + await self.web_select(By.ID, "pstad-citychsr", option_text) + break + except TimeoutError: + LOG.debug("Could not set city from location") ############################# # set contact street