diff --git a/app.py b/app.py new file mode 100644 index 0000000..7af4856 --- /dev/null +++ b/app.py @@ -0,0 +1,60 @@ +import csv +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.firefox.service import Service +from selenium.webdriver.support.ui import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from webdriver_manager.firefox import GeckoDriverManager + +# Set up the Firefox WebDriver using Service +service = Service(GeckoDriverManager().install()) # Automatically manage geckodriver + +# Set up the Firefox options (you can use headless if you want) +options = webdriver.FirefoxOptions() + +# Initialize the Firefox WebDriver with the correct Service and Options +driver = webdriver.Firefox(service=service, options=options) + +# URL where the form is located +url = "" + +# Open the webpage +driver.get(url) + +# Wait until the page is loaded and the element is present +wait = WebDriverWait(driver, 10) + +# Mapping of CSV values to radio button IDs +radio_button_map = { + 'individual': 'id25', + 'organization': 'id27', + 'professional': 'id29', + 'municipality': 'id31', + 'agency': 'id33' +} + +# Read the CSV file +csv_file_path = "surveys/test.csv" +with open(csv_file_path, mode='r') as file: + csv_reader = csv.DictReader(file) + + for row in csv_reader: + who_planted = row['Who Planted The Tree(s)?'].lower() # Convert to lowercase for consistency + + # Check if the value is in the radio button map + if who_planted in radio_button_map: + radio_button_id = radio_button_map[who_planted] + try: + # Wait for the radio button to be clickable and click it + radio_button = wait.until(EC.element_to_be_clickable((By.ID, radio_button_id))) + radio_button.click() + except Exception as e: + print(f"Error selecting '{who_planted}' radio button (ID: {radio_button_id}): {e}") + else: + print(f"Unknown value in CSV: '{who_planted}', skipping...") + +# Optional: Submit the form if necessary +# driver.find_element(By.ID, 'submit_button_id').click() + +# Close the driver +driver.quit()