From b05323f41a4116dd3b249637404a592759831ca4 Mon Sep 17 00:00:00 2001 From: Nick Hepler Date: Thu, 20 Feb 2025 22:36:04 -0500 Subject: [PATCH] Complete first quesrion, select button, create response vars. --- app.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/app.py b/app.py index 7af4856..615a4b2 100644 --- a/app.py +++ b/app.py @@ -25,7 +25,7 @@ driver.get(url) wait = WebDriverWait(driver, 10) # Mapping of CSV values to radio button IDs -radio_button_map = { +who_planted_the_trees_map = { 'individual': 'id25', 'organization': 'id27', 'professional': 'id29', @@ -36,25 +36,37 @@ radio_button_map = { # 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 + surveys = csv.DictReader(file) + # Iterate through each survey in the CSV. + for survey in surveys: - # 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() + # Get responses to qestions. + who_planted = survey['Who Planted The Tree(s)?'].lower() + start_date = survey['Start Date of Planting'] + end_date = survey['End Date of Planting'] + num_trees = survey['End Date of Planting'] + + # Answer the Who Planted The Tree(s)? question. + radio_button_id = who_planted_the_trees_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}") + # Click Next button. + driver.find_element(By.XPATH, '/html/body/div[3]/section[1]/div/div[6]/div[1]/button[2]').click() + # Answer the Start Date of Planting question. + radio_button_id = who_planted_the_trees_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.XPATH, '/html/body/div[3]/section[1]/div/div[6]/div[1]/button[2]'))) + radio_button.click() + except Exception as e: + print(f"Error selecting '{who_planted}' radio button (ID: {radio_button_id}): {e}") + # Click Next button. + driver.find_element(By.XPATH, '/html/body/div[3]/section[1]/div/div[6]/div[1]/button[2]').click() + # Close the driver driver.quit()