Skip to content

Instantly share code, notes, and snippets.

@tibers
Last active June 11, 2025 13:02
Show Gist options
  • Save tibers/c645cbc234b916b2584820c4d1f7936f to your computer and use it in GitHub Desktop.
Save tibers/c645cbc234b916b2584820c4d1f7936f to your computer and use it in GitHub Desktop.
nokings form stuffer
#!/usr/bin/env python3
import random
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
def generate_random_name():
"""Generate a random first and last name"""
first_names = [
"Sarah", "Michael", "Emma", "David", "Jessica", "John", "Ashley",
"Christopher", "Amanda", "Matthew", "Jennifer", "Daniel", "Lisa",
"Andrew", "Michelle", "Joshua", "Kimberly", "Ryan", "Amy", "James"
]
last_names = [
"Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis",
"Rodriguez", "Martinez", "Hernandez", "Lopez", "Gonzalez", "Wilson",
"Anderson", "Thomas", "Taylor", "Moore", "Jackson", "Martin", "Lee"
]
first = random.choice(first_names)
last = random.choice(last_names)
return first, last
def generate_phone_number():
"""Generate a random US phone number in XXX-XXX-XXXX format"""
# First digit of area code can't be 0 or 1
area_code = str(random.randint(2, 9)) + str(random.randint(0, 9)) + str(random.randint(0, 9))
# First digit of exchange can't be 0 or 1
exchange = str(random.randint(2, 9)) + str(random.randint(0, 9)) + str(random.randint(0, 9))
# Last four digits
number = str(random.randint(0, 9999)).zfill(4)
return f"{area_code}-{exchange}-{number}"
def generate_email(first_name, last_name):
"""Generate email in format [email protected]"""
return f"{first_name.upper()}.{last_name.upper()}@GMAIL.COM"
def setup_driver():
"""Setup Chrome WebDriver with options"""
chrome_options = Options()
# Remove headless mode to see the browser in action
# chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(options=chrome_options)
return driver
def fill_form(url):
"""Fill out the form with random data"""
driver = setup_driver()
try:
# Navigate to the page
print(f"Navigating to: {url}")
driver.get(url)
# Wait for page to load
wait = WebDriverWait(driver, 10)
# Generate random test data
first_name, last_name = generate_random_name()
phone = generate_phone_number()
email = generate_email(first_name, last_name)
print(f"Generated test data:")
print(f"Name: {first_name} {last_name}")
print(f"Phone: {phone}")
print(f"Email: {email}")
# Material-UI specific selectors - look for labels first, then find corresponding inputs
def find_mui_field_by_label(label_text):
"""Find MUI input field by its label text"""
try:
# Find the label containing the text
label = driver.find_element(By.XPATH, f"//label[contains(text(), '{label_text}')]")
label_for = label.get_attribute("for")
if label_for:
# Find the input with the corresponding id
input_field = driver.find_element(By.ID, label_for)
return input_field
except:
pass
return None
# Common form field selectors to try (including MUI patterns)
name_selectors = [
"input[name*='name']",
"input[id*='name']",
"input[placeholder*='name']",
"input[type='text']",
"input[id*='mbz-input']" # MUI mobilize pattern
]
phone_selectors = [
"input[name*='phone']",
"input[id*='phone']",
"input[placeholder*='phone']",
"input[type='tel']",
"input[id*='mbz-input']" # MUI mobilize pattern
]
email_selectors = [
"input[name*='email']",
"input[id*='email']",
"input[placeholder*='email']",
"input[type='email']",
"input[id*='mbz-input']" # MUI mobilize pattern
]
# Try to fill first name field
first_name_filled = False
# First try MUI label approach
first_name_field = find_mui_field_by_label("First name")
if first_name_field:
try:
first_name_field.clear()
first_name_field.send_keys(first_name)
print("Filled first name field using MUI label approach")
first_name_filled = True
except Exception as e:
print(f"Error filling first name via MUI label: {e}")
# If MUI approach failed, try other selectors
if not first_name_filled:
for selector in name_selectors:
try:
name_field = driver.find_element(By.CSS_SELECTOR, selector)
name_field.clear()
name_field.send_keys(first_name)
print(f"Filled first name field using selector: {selector}")
first_name_filled = True
break
except:
continue
if not first_name_filled:
print("Could not find first name field")
# Try to fill last name field
last_name_filled = False
# First try MUI label approach
last_name_field = find_mui_field_by_label("Last name")
if last_name_field:
try:
last_name_field.clear()
last_name_field.send_keys(last_name)
print("Filled last name field using MUI label approach")
last_name_filled = True
except Exception as e:
print(f"Error filling last name via MUI label: {e}")
if not last_name_filled:
print("Could not find last name field")
# Try to fill phone field
phone_filled = False
# First try MUI label approach
phone_field = find_mui_field_by_label("Phone")
if not phone_field:
phone_field = find_mui_field_by_label("Phone number")
if not phone_field:
phone_field = find_mui_field_by_label("Mobile phone")
if phone_field:
try:
phone_field.clear()
phone_field.send_keys(phone)
print("Filled phone field using MUI label approach")
phone_filled = True
except Exception as e:
print(f"Error filling phone via MUI label: {e}")
# If MUI approach failed, try other selectors
if not phone_filled:
for selector in phone_selectors:
try:
phone_field = driver.find_element(By.CSS_SELECTOR, selector)
phone_field.clear()
phone_field.send_keys(phone)
print(f"Filled phone field using selector: {selector}")
phone_filled = True
break
except:
continue
if not phone_filled:
print("Could not find phone field")
# Try to fill email field
email_filled = False
# First try MUI label approach
email_field = find_mui_field_by_label("Email")
if not email_field:
email_field = find_mui_field_by_label("Email address")
if email_field:
try:
email_field.clear()
email_field.send_keys(email)
print("Filled email field using MUI label approach")
email_filled = True
except Exception as e:
print(f"Error filling email via MUI label: {e}")
# If MUI approach failed, try other selectors
if not email_filled:
for selector in email_selectors:
try:
email_field = driver.find_element(By.CSS_SELECTOR, selector)
email_field.clear()
email_field.send_keys(email)
print(f"Filled email field using selector: {selector}")
email_filled = True
break
except:
continue
if not email_filled:
print("Could not find email field")
# Wait a moment to see the filled form
time.sleep(2)
# Try to find and click submit button
submit_selectors = [
'button[data-testid="actionbutton-submit"]', # MUI specific selector
"input[type='submit']",
"button[type='submit']",
"button:contains('Attend')",
"button:contains('Submit')",
"button:contains('Sign Up')",
"button:contains('Register')"
]
submitted = False
# First try the specific MUI selector
try:
submit_button = driver.find_element(By.CSS_SELECTOR, 'button[data-testid="actionbutton-submit"]')
print("Found submit button using MUI data-testid selector")
# Uncomment the next line to actually submit the form
# submit_button.click()
print("Form submission disabled for safety - uncomment line above to enable")
submitted = True
except Exception as e:
print(f"Could not find submit button via data-testid: {e}")
# If MUI approach failed, try other selectors
if not submitted:
for selector in submit_selectors[1:]: # Skip the first one as we already tried it
try:
submit_button = driver.find_element(By.CSS_SELECTOR, selector)
print(f"Found submit button: {selector}")
submit_button.click()
submitted = True
break
except:
continue
if not submitted:
print("Could not find submit button")
# Keep browser open for inspection
input("Press Enter to close browser...")
except Exception as e:
print(f"Error occurred: {str(e)}")
finally:
driver.quit()
if __name__ == "__main__":
# URL of the form to test
# add your own even numbers for, uh, testing
url = "https://www.mobilize.us/nokings/event/799398/"
print(f"Target URL: {url}")
print()
# Run the form filling
fill_form(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment