Complete first quesrion, select button, create response vars.

This commit is contained in:
Nick Hepler 2025-02-20 22:36:04 -05:00
parent 0060d189d9
commit b05323f41a

50
app.py
View File

@ -25,7 +25,7 @@ driver.get(url)
wait = WebDriverWait(driver, 10) wait = WebDriverWait(driver, 10)
# Mapping of CSV values to radio button IDs # Mapping of CSV values to radio button IDs
radio_button_map = { who_planted_the_trees_map = {
'individual': 'id25', 'individual': 'id25',
'organization': 'id27', 'organization': 'id27',
'professional': 'id29', 'professional': 'id29',
@ -36,25 +36,37 @@ radio_button_map = {
# Read the CSV file # Read the CSV file
csv_file_path = "surveys/test.csv" csv_file_path = "surveys/test.csv"
with open(csv_file_path, mode='r') as file: with open(csv_file_path, mode='r') as file:
csv_reader = csv.DictReader(file) surveys = csv.DictReader(file)
# Iterate through each survey in the CSV.
for row in csv_reader: for survey in surveys:
who_planted = row['Who Planted The Tree(s)?'].lower() # Convert to lowercase for consistency
# Check if the value is in the radio button map # Get responses to qestions.
if who_planted in radio_button_map: who_planted = survey['Who Planted The Tree(s)?'].lower()
radio_button_id = radio_button_map[who_planted] start_date = survey['Start Date of Planting']
try: end_date = survey['End Date of Planting']
# Wait for the radio button to be clickable and click it num_trees = survey['End Date of Planting']
radio_button = wait.until(EC.element_to_be_clickable((By.ID, radio_button_id)))
radio_button.click() # Answer the Who Planted The Tree(s)? question.
except Exception as e: radio_button_id = who_planted_the_trees_map[who_planted]
print(f"Error selecting '{who_planted}' radio button (ID: {radio_button_id}): {e}") try:
else: # Wait for the radio button to be clickable and click it
print(f"Unknown value in CSV: '{who_planted}', skipping...") radio_button = wait.until(EC.element_to_be_clickable((By.ID, radio_button_id)))
radio_button.click()
# Optional: Submit the form if necessary except Exception as e:
# driver.find_element(By.ID, 'submit_button_id').click() 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 # Close the driver
driver.quit() driver.quit()