0
0
Selenium Pythontesting~15 mins

Submitting forms in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Submit Contact Us form and verify submission success
Preconditions (1)
Step 1: Enter 'John Doe' in the Name input field with id 'name'
Step 2: Enter 'john.doe@example.com' in the Email input field with id 'email'
Step 3: Enter 'Hello, this is a test message.' in the Message textarea with id 'message'
Step 4: Click the Submit button with id 'submit-btn'
✅ Expected Result: The page shows a success message with id 'success-msg' containing text 'Thank you for contacting us!'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the success message element is visible after form submission
Verify the success message text equals 'Thank you for contacting us!'
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use By.ID locator strategy for stable element identification
Use Page Object Model pattern for maintainability (optional for this exercise)
Avoid hardcoded sleeps; use WebDriverWait instead
Automated Solution
Selenium Python
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.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different sets of form inputs to verify multiple submissions.

Show Hint