0
0
Selenium Pythontesting~5 mins

Why form handling is common in testing in Selenium Python

Choose your learning style9 modes available
Introduction

Forms let users send information on websites. Testing forms ensures data is sent correctly and the site works well.

When checking if a login form accepts correct username and password
When verifying a signup form saves new user details properly
When testing a contact form sends messages to the right place
When ensuring error messages show for missing or wrong inputs
When confirming that form buttons and fields respond as expected
Syntax
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Open browser and go to page
browser = webdriver.Chrome()
browser.get('https://example.com/form')

# Find form fields and fill them
input_name = browser.find_element(By.ID, 'name')
input_name.send_keys('John Doe')

input_email = browser.find_element(By.ID, 'email')
input_email.send_keys('john@example.com')

# Submit the form
submit_button = browser.find_element(By.ID, 'submit')
submit_button.click()

Use clear locators like ID or name to find form elements.

Always close the browser after testing to free resources.

Examples
Fill a password field using the 'name' attribute locator.
Selenium Python
input_password = browser.find_element(By.NAME, 'password')
input_password.send_keys('mypassword123')
Click the submit button using a CSS selector.
Selenium Python
submit_button = browser.find_element(By.CSS_SELECTOR, 'button[type="submit"]')
submit_button.click()
Sample Program

This script opens a form page, fills name and email, submits the form, waits for a confirmation message, prints it, then closes the browser.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

browser = webdriver.Chrome()
browser.get('https://example.com/form')

# Fill name
name_field = browser.find_element(By.ID, 'name')
name_field.send_keys('Alice')

# Fill email
email_field = browser.find_element(By.ID, 'email')
email_field.send_keys('alice@example.com')

# Submit form
submit_btn = browser.find_element(By.ID, 'submit')
submit_btn.click()

# Wait for confirmation message
time.sleep(2)

# Check confirmation text
confirmation = browser.find_element(By.ID, 'confirmation').text
print(confirmation)

browser.quit()
OutputSuccess
Important Notes

Always wait for page elements to load before interacting to avoid errors.

Use explicit waits instead of fixed sleep for better reliability.

Test both valid and invalid inputs to cover all cases.

Summary

Forms are key for user input on websites.

Testing forms ensures data is handled correctly and user experience is smooth.

Selenium helps automate filling and submitting forms for testing.