fix: bypass attempts to make the app not working
This commit is contained in:
141
main.py
141
main.py
@@ -24,14 +24,17 @@ logger = setup_logging()
|
||||
|
||||
def close_cookie_consent(page):
|
||||
try:
|
||||
logger.info("Waiting for cookie consent dialog to appear...")
|
||||
reject_button = page.locator("#onetrust-reject-all-handler")
|
||||
if reject_button.is_visible(timeout=5000):
|
||||
reject_button.click()
|
||||
logger.info("Cookie consent dialog closed")
|
||||
else:
|
||||
logger.warning("Cookie consent dialog not found")
|
||||
|
||||
# Wait longer for the cookie banner to appear after page load
|
||||
reject_button.wait_for(state="visible", timeout=15000)
|
||||
reject_button.click()
|
||||
logger.info("Cookie consent dialog closed")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Could not close cookie dialog: {e}")
|
||||
logger.warning(f"Cookie consent dialog not found or timed out: {e}")
|
||||
# Continue execution even if cookie dialog is not found
|
||||
|
||||
|
||||
def close_popup_modal(page):
|
||||
@@ -250,33 +253,110 @@ def get_image_path(specified_image=None, images_folder="images"):
|
||||
return None
|
||||
|
||||
|
||||
def fill_form_field(page, field_identifiers, value, field_name):
|
||||
"""Try multiple strategies to fill a form field"""
|
||||
if not value:
|
||||
return
|
||||
|
||||
for identifier in field_identifiers:
|
||||
try:
|
||||
# Try to locate the field
|
||||
field = page.locator(identifier)
|
||||
if field.count() > 0:
|
||||
field.fill(str(value))
|
||||
logger.info(f"Successfully filled {field_name} using {identifier}")
|
||||
return
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
logger.warning(f"Could not find field for {field_name}")
|
||||
|
||||
|
||||
def upload_image_field(page, file_identifiers, image_path):
|
||||
"""Try multiple strategies to upload an image"""
|
||||
if not image_path:
|
||||
return
|
||||
|
||||
for identifier in file_identifiers:
|
||||
try:
|
||||
field = page.locator(identifier)
|
||||
if field.count() > 0:
|
||||
field.set_input_files(image_path)
|
||||
logger.info(f"Successfully uploaded image using {identifier}")
|
||||
return
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
logger.warning("Could not find file upload field")
|
||||
|
||||
|
||||
def fill_form(page, form_data):
|
||||
try:
|
||||
logger.info("Filling form with data...")
|
||||
|
||||
# Fill required fields using Spanish field names
|
||||
page.fill("#form-field-local_name", form_data["nombre_local"])
|
||||
page.fill("#form-field-local_street", form_data["direccion"])
|
||||
page.fill("#form-field-local_postal", str(form_data["codigo_postal"]))
|
||||
page.fill("#form-field-local_localy", form_data["municipio"])
|
||||
# Define multiple possible selectors for each field (in order of preference)
|
||||
field_mappings = {
|
||||
"nombre_local": [
|
||||
"#form-field-local__name", # New format
|
||||
"#form-field-local_name", # Old format
|
||||
"input[placeholder*='Nombre del local']",
|
||||
"input[name*='local'][name*='name']"
|
||||
],
|
||||
"direccion": [
|
||||
"#form-field-local__street",
|
||||
"#form-field-local_street",
|
||||
"input[placeholder*='Dirección del local']",
|
||||
"input[name*='local'][name*='street']"
|
||||
],
|
||||
"codigo_postal": [
|
||||
"#form-field-local__postal",
|
||||
"#form-field-local_postal",
|
||||
"input[placeholder*='Código Postal']",
|
||||
"input[name*='local'][name*='postal']"
|
||||
],
|
||||
"municipio": [
|
||||
"#form-field-local__localy",
|
||||
"#form-field-local_localy",
|
||||
"input[placeholder*='Municipio']",
|
||||
"input[name*='local'][name*='local']"
|
||||
],
|
||||
"evento_deportivo": [
|
||||
"#form-field-field__evento",
|
||||
"#form-field-field_evento",
|
||||
"textarea[placeholder*='evento deportivo']",
|
||||
"textarea[name*='evento']"
|
||||
],
|
||||
"descripcion": [
|
||||
"#form-field-field__message",
|
||||
"#form-field-field_message",
|
||||
"textarea[placeholder*='situación a denunciar']",
|
||||
"textarea[name*='message']"
|
||||
],
|
||||
"email_contacto": [
|
||||
"#form-field-field_email",
|
||||
"input[type='email']",
|
||||
"input[placeholder*='Email']"
|
||||
]
|
||||
}
|
||||
|
||||
# Fill optional fields if provided
|
||||
if "evento_deportivo" in form_data and form_data["evento_deportivo"]:
|
||||
page.fill("#form-field-field_evento", form_data["evento_deportivo"])
|
||||
|
||||
if "descripcion" in form_data and form_data["descripcion"]:
|
||||
page.fill("#form-field-field_message", form_data["descripcion"])
|
||||
|
||||
if "email_contacto" in form_data and form_data["email_contacto"]:
|
||||
page.fill("#form-field-field_email", form_data["email_contacto"])
|
||||
# Fill each field using fallback strategies
|
||||
for field_key, selectors in field_mappings.items():
|
||||
if field_key in form_data:
|
||||
fill_form_field(page, selectors, form_data[field_key], field_key)
|
||||
|
||||
# Upload specified or random image
|
||||
# Handle image upload with fallback strategies
|
||||
specified_image = form_data.get("imagen")
|
||||
image_path = get_image_path(specified_image)
|
||||
if image_path:
|
||||
logger.info("Uploading image...")
|
||||
page.set_input_files("#form-field-local_files", image_path)
|
||||
logger.info("Image uploaded successfully")
|
||||
file_selectors = [
|
||||
"#form-field-local_files",
|
||||
"input[type='file']",
|
||||
"input[name*='local_files']",
|
||||
".elementor-upload-field"
|
||||
]
|
||||
upload_image_field(page, file_selectors, image_path)
|
||||
logger.info("Image upload attempted")
|
||||
|
||||
logger.info("Form filled successfully")
|
||||
|
||||
@@ -301,9 +381,18 @@ def submit_form(page):
|
||||
def return_to_form(page):
|
||||
try:
|
||||
logger.info("Returning to form...")
|
||||
return_button = page.locator("a:has-text('Volver a Denuncias')")
|
||||
return_button.wait_for(state="visible", timeout=5000)
|
||||
return_button.click()
|
||||
|
||||
# Try clicking the return button first
|
||||
try:
|
||||
return_button = page.locator("a:has-text('Volver')")
|
||||
return_button.wait_for(state="visible", timeout=5000)
|
||||
return_button.click()
|
||||
logger.info("Clicked return button")
|
||||
except Exception:
|
||||
# Fallback: navigate directly to the form URL
|
||||
logger.info("Return button not found, navigating directly to form URL")
|
||||
page.goto("https://laligabares.com/denuncias/")
|
||||
|
||||
logger.info("Returned to form page")
|
||||
|
||||
except Exception as e:
|
||||
|
Reference in New Issue
Block a user