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):
|
def close_cookie_consent(page):
|
||||||
try:
|
try:
|
||||||
|
logger.info("Waiting for cookie consent dialog to appear...")
|
||||||
reject_button = page.locator("#onetrust-reject-all-handler")
|
reject_button = page.locator("#onetrust-reject-all-handler")
|
||||||
if reject_button.is_visible(timeout=5000):
|
|
||||||
reject_button.click()
|
# Wait longer for the cookie banner to appear after page load
|
||||||
logger.info("Cookie consent dialog closed")
|
reject_button.wait_for(state="visible", timeout=15000)
|
||||||
else:
|
reject_button.click()
|
||||||
logger.warning("Cookie consent dialog not found")
|
logger.info("Cookie consent dialog closed")
|
||||||
|
|
||||||
except Exception as e:
|
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):
|
def close_popup_modal(page):
|
||||||
@@ -250,33 +253,110 @@ def get_image_path(specified_image=None, images_folder="images"):
|
|||||||
return None
|
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):
|
def fill_form(page, form_data):
|
||||||
try:
|
try:
|
||||||
logger.info("Filling form with data...")
|
logger.info("Filling form with data...")
|
||||||
|
|
||||||
# Fill required fields using Spanish field names
|
# Define multiple possible selectors for each field (in order of preference)
|
||||||
page.fill("#form-field-local_name", form_data["nombre_local"])
|
field_mappings = {
|
||||||
page.fill("#form-field-local_street", form_data["direccion"])
|
"nombre_local": [
|
||||||
page.fill("#form-field-local_postal", str(form_data["codigo_postal"]))
|
"#form-field-local__name", # New format
|
||||||
page.fill("#form-field-local_localy", form_data["municipio"])
|
"#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
|
# Fill each field using fallback strategies
|
||||||
if "evento_deportivo" in form_data and form_data["evento_deportivo"]:
|
for field_key, selectors in field_mappings.items():
|
||||||
page.fill("#form-field-field_evento", form_data["evento_deportivo"])
|
if field_key in form_data:
|
||||||
|
fill_form_field(page, selectors, form_data[field_key], field_key)
|
||||||
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"])
|
|
||||||
|
|
||||||
# Upload specified or random image
|
# Handle image upload with fallback strategies
|
||||||
specified_image = form_data.get("imagen")
|
specified_image = form_data.get("imagen")
|
||||||
image_path = get_image_path(specified_image)
|
image_path = get_image_path(specified_image)
|
||||||
if image_path:
|
if image_path:
|
||||||
logger.info("Uploading image...")
|
logger.info("Uploading image...")
|
||||||
page.set_input_files("#form-field-local_files", image_path)
|
file_selectors = [
|
||||||
logger.info("Image uploaded successfully")
|
"#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")
|
logger.info("Form filled successfully")
|
||||||
|
|
||||||
@@ -301,9 +381,18 @@ def submit_form(page):
|
|||||||
def return_to_form(page):
|
def return_to_form(page):
|
||||||
try:
|
try:
|
||||||
logger.info("Returning to form...")
|
logger.info("Returning to form...")
|
||||||
return_button = page.locator("a:has-text('Volver a Denuncias')")
|
|
||||||
return_button.wait_for(state="visible", timeout=5000)
|
# Try clicking the return button first
|
||||||
return_button.click()
|
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")
|
logger.info("Returned to form page")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
Reference in New Issue
Block a user