Skip to content

Instantly share code, notes, and snippets.

@hskrasek
Last active March 1, 2025 20:45
Show Gist options
  • Save hskrasek/277c5d94a7c2fec3b0bdaa23845df8f5 to your computer and use it in GitHub Desktop.
Save hskrasek/277c5d94a7c2fec3b0bdaa23845df8f5 to your computer and use it in GitHub Desktop.
An educational dive into writing a Selenium script for testing form submissions, using Python.

Prerequisites

  • Python 3
  • Selenium

Running

python app.py
import os.path
import random
import string
from selenium import webdriver
from selenium.common import ElementClickInterceptedException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
def generate_random_email(domain="example.com", length=10):
"""Generate a random email address."""
local_part = ''.join(random.choices(string.ascii_lowercase + string.digits, k=length))
return f"{local_part}@{domain}"
# Set up the Selenium driver (ensure you have the correct driver executable in your PATH)
options = webdriver.ChromeOptions()
options.add_argument("--headless=new")
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options) # or webdriver.Firefox(), etc.
# Navigate to the target web form
driver.get("https://enddei.ed.gov/")
initial_url = driver.current_url
# Wait for the page to load (adjust the timeout as needed)
wait = WebDriverWait(driver, 10)
# === Auto-populate form fields ===
# Example: Populate an email field (adjust the locator as necessary)
email_field = wait.until(EC.presence_of_element_located((By.NAME, "email")))
email_field.send_keys(generate_random_email())
# You can continue to add more fields here following the pattern above.
# For instance:
location_field = driver.find_element(By.NAME, "location")
location_field.send_keys("Keller")
zip_codes = ["76244", "76248", "76262"]
zip_code_field = driver.find_element(By.NAME, "zipcode")
zip_code_field.send_keys(random.choice(zip_codes))
description = """
Trans rights are human rights because they affirm the intrinsic dignity and equality of every person. Protecting trans rights means safeguarding freedom from discrimination and violence, and fostering inclusive communities. Every trans person deserves to live authentically, with access to the same opportunities and respect as everyone else. Upholding these rights enriches society and strengthens our commitment to justice for all. Rise with pride.
"""
description_field = driver.find_element(By.NAME, "description")
description_field.send_keys(description)
# === File Upload ===
# Assume there is a file input element with the name attribute 'attachment'
# Adjust the locator based on the actual page's HTML.
upload_field = driver.find_element(By.NAME, "file")
# Provide the full path to the file you wish to upload.
# For this exercise, assume 'trans_rights.pdf' is a 5MB file containing 34 pages of "Trans Rights are Human Rights".
file_path = os.path.abspath("trans-rights.pdf")
upload_field.send_keys(file_path)
# === Submit the Form ===
# Locate the submit button (again, adjust the locator to match the page's markup)
try:
wait = WebDriverWait(driver, 20)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[@type='submit']")))
driver.execute_script("arguments[0].scrollIntoView(true);", element)
driver.execute_script("arguments[0].click();", element)
except ElementClickInterceptedException as e:
print("ElementClickInterceptedException caught:", e)
# Save a screenshot for debugging
driver.save_screenshot("debug_exception.png")
else:
# Optionally, wait for a confirmation message or page redirection before closing the driver.
# For example:
wait.until(lambda driver: driver.current_url != initial_url)
# Close the driver
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment