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
This commit is contained in:
Jens Bergmann
2025-01-17 22:18:50 +01:00
committed by Sebastian Thomschke
parent 46e585b96d
commit 2f93e0dfda

View File

@@ -668,6 +668,18 @@ class KleinanzeigenBot(WebScrapingMixin):
############################# #############################
if ad_cfg["contact"]["zipcode"]: if ad_cfg["contact"]["zipcode"]:
await self.web_input(By.ID, "pstad-zip", 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 # set contact street