Submitting forms in Selenium Python - Build an Automation Script
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 def test_submit_contact_form(): driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) try: # Open Contact Us page driver.get('https://example.com/contact') # Fill Name field name_input = wait.until(EC.visibility_of_element_located((By.ID, 'name'))) name_input.clear() name_input.send_keys('John Doe') # Fill Email field email_input = wait.until(EC.visibility_of_element_located((By.ID, 'email'))) email_input.clear() email_input.send_keys('john.doe@example.com') # Fill Message textarea message_input = wait.until(EC.visibility_of_element_located((By.ID, 'message'))) message_input.clear() message_input.send_keys('Hello, this is a test message.') # Click Submit button submit_button = wait.until(EC.element_to_be_clickable((By.ID, 'submit-btn'))) submit_button.click() # Wait for success message success_msg = wait.until(EC.visibility_of_element_located((By.ID, 'success-msg'))) # Assert success message text assert success_msg.text == 'Thank you for contacting us!', f"Expected success message text not found. Got: {success_msg.text}" finally: driver.quit()
This test script uses Selenium WebDriver with Python to automate the form submission.
First, it opens the Contact Us page URL.
Then it waits explicitly for each input field to be visible before interacting. This avoids errors if the page loads slowly.
It clears any existing text and enters the specified values for Name, Email, and Message fields.
Next, it waits for the Submit button to be clickable and clicks it.
After submission, it waits for the success message element to appear and verifies its text matches the expected confirmation message.
Finally, the browser is closed in the finally block to ensure cleanup even if assertions fail.
This approach uses explicit waits and By.ID locators for reliability and maintainability.
Now add data-driven testing with 3 different sets of form inputs to verify multiple submissions.
Practice
submit() method do in Selenium when testing forms?Solution
Step 1: Understand the purpose of
Thesubmit()submit()method is used to send the form data to the server by submitting the form element.Step 2: Compare with other options
Clearing inputs, refreshing pages, or closing windows are not actions performed bysubmit().Final Answer:
It sends the form data to the server by submitting the form. -> Option AQuick Check:
submit() sends form data [OK]
- Confusing submit() with clear() method
- Thinking submit() refreshes the page
- Assuming submit() closes the browser
Solution
Step 1: Identify correct Selenium syntax for submitting a form
The correct method is to locate the form element and callsubmit()on it, likedriver.find_element(By.ID, 'form').submit().Step 2: Check other options for errors
Options A and B use invalid method names. driver.find_element(By.ID, 'form').send_keys(Keys.ENTER) sends ENTER key but does not explicitly submit the form element.Final Answer:
driver.find_element(By.ID, 'form').submit() -> Option DQuick Check:
Use element.submit() to submit form [OK]
- Using driver.submit() which does not exist
- Trying to submit with send_keys instead of submit()
- Calling submit on driver instead of element
form = driver.find_element(By.NAME, 'loginForm') form.submit()
Solution
Step 1: Locate the form element by name
The code finds the form element with name 'loginForm' usingfind_element(By.NAME, 'loginForm').Step 2: Call submit() on the form element
Callingsubmit()on the form element sends the form data to the server, triggering submission.Final Answer:
The form with name 'loginForm' will be submitted to the server. -> Option AQuick Check:
form.submit() submits form [OK]
- Thinking submit() is invalid method
- Assuming submit() does nothing
- Confusing submit() with browser close
form = driver.find_element(By.ID, 'signupForm') form.submit
Why does the form not submit when you run it?
Solution
Step 1: Check method call syntax
The code usesform.submitwithout parentheses, so the method is referenced but not executed.Step 2: Understand effect of missing parentheses
Without parentheses, the form does not submit because the method is not called. Adding()calls the method.Final Answer:
Because submit() method was not called with parentheses. -> Option BQuick Check:
Call submit() with () to execute [OK]
- Forgetting parentheses after submit
- Assuming submit is a property, not a method
- Not verifying form locator correctness
Solution
Step 1: Locate input inside the form
You can find an input element inside the form usingfind_element(By.NAME, 'email').Step 2: Call submit() on the input element
Callingsubmit()on any element inside the form submits the entire form.Final Answer:
input_element = driver.find_element(By.NAME, 'email') input_element.submit() -> Option CQuick Check:
submit() on input submits form [OK]
- Trying to call driver.submit() which does not exist
- Using send_keys(Keys.RETURN) which may not submit form
- Not calling submit() on an element
Ready to prove it?
Pass the test and this pattern moves unknown
