fix: failed to set special attributes #334 (#370)

This commit is contained in:
Heavenfighter
2025-01-09 17:01:48 +01:00
committed by GitHub
parent e590a32aa2
commit f9eb6185c7
2 changed files with 26 additions and 12 deletions

View File

@@ -819,15 +819,26 @@ class KleinanzeigenBot(WebScrapingMixin):
pass # nosec pass # nosec
try: try:
await self.web_select(By.XPATH, f"//select[contains(@id, '{special_attribute_key}')]", special_attribute_value) # finding element by name cause id are composed sometimes eg. autos.marke_s+autos.model_s for Modell by cars
except TimeoutError: special_attr_elem = await self.web_find(By.XPATH, f"//*[contains(@name, '{special_attribute_key}')]")
LOG.debug("Attribute field '%s' is not of kind dropdown, trying to input as plain text...", special_attribute_key) except TimeoutError as ex:
LOG.debug("Attribute field '%s' could not be found.", special_attribute_key)
raise TimeoutError(f"Failed to set special attribute [{special_attribute_key}] (not found)") from ex
# workaround for https://github.com/Second-Hand-Friends/kleinanzeigen-bot/issues/368
elem_id = getattr(special_attr_elem.attrs, 'id')
elem_id = ''.join(['\\' + c if c in '!"#$%&\'()*+,./:;<=>?@[\\]^`{|}~' else c for c in elem_id])
try: try:
await self.web_input(By.ID, special_attribute_key, special_attribute_value) if special_attr_elem.local_name == 'select':
except TimeoutError: LOG.debug("Attribute field '%s' seems to be a select...", special_attribute_key)
LOG.debug("Attribute field '%s' is not of kind plain text, trying to input as radio button...", special_attribute_key) await self.web_select(By.ID, elem_id, special_attribute_value)
try: elif getattr(special_attr_elem.attrs, 'type') == 'checkbox':
await self.web_click(By.XPATH, f"//*[contains(@id, '{special_attribute_key}')]/option[@value='{special_attribute_value}']") LOG.debug("Attribute field '%s' seems to be a checkbox...", special_attribute_key)
await self.web_click(By.ID, elem_id)
else:
LOG.debug("Attribute field '%s' seems to be a text input...", special_attribute_key)
await self.web_input(By.ID, elem_id, special_attribute_value)
except TimeoutError as ex: except TimeoutError as ex:
LOG.debug("Attribute field '%s' is not of kind radio button.", special_attribute_key) LOG.debug("Attribute field '%s' is not of kind radio button.", special_attribute_key)
raise TimeoutError(f"Failed to set special attribute [{special_attribute_key}]") from ex raise TimeoutError(f"Failed to set special attribute [{special_attribute_key}]") from ex

View File

@@ -245,6 +245,9 @@ class AdExtractor(WebScrapingMixin):
# change e.g. category "161/172" to "161/172/lautsprecher_kopfhoerer" # change e.g. category "161/172" to "161/172/lautsprecher_kopfhoerer"
info['category'] = f"{info['category']}/{info['special_attributes']['art_s']}" info['category'] = f"{info['category']}/{info['special_attributes']['art_s']}"
del info['special_attributes']['art_s'] del info['special_attributes']['art_s']
if "schaden_s" in info['special_attributes']:
# change f to 'nein' and 't' to 'ja'
info['special_attributes']['schaden_s'] = info['special_attributes']['schaden_s'].translate(str.maketrans({'t': 'ja', 'f': 'nein'}))
info['price'], info['price_type'] = await self._extract_pricing_info_from_ad_page() info['price'], info['price_type'] = await self._extract_pricing_info_from_ad_page()
info['shipping_type'], info['shipping_costs'], info['shipping_options'] = await self._extract_shipping_info_from_ad_page() info['shipping_type'], info['shipping_costs'], info['shipping_options'] = await self._extract_shipping_info_from_ad_page()
info['sell_directly'] = await self._extract_sell_directly_from_ad_page() info['sell_directly'] = await self._extract_sell_directly_from_ad_page()