0
0
Selenium-pythonHow-ToBeginner · 4 min read

How to Automate Form Filling with Selenium: Simple Guide

To automate form filling with Selenium, locate form fields using find_element methods, then use send_keys() to input text. Finally, submit the form by clicking the submit button or using submit().
📐

Syntax

Use driver.find_element(By.METHOD, 'locator') to find form fields. Use send_keys('text') to enter text into input fields. Use click() to press buttons or submit() to submit forms.

  • driver: your Selenium WebDriver instance
  • By.METHOD: locator strategy like ID, NAME, or CSS_SELECTOR
  • send_keys: types text into the field
  • click: clicks a button or link
python
from selenium import webdriver
from selenium.webdriver.common.by import By

# Initialize WebDriver
driver = webdriver.Chrome()

# Open the webpage
driver.get('https://example.com/form')

# Find input field by name and type text
input_field = driver.find_element(By.NAME, 'username')
input_field.send_keys('myusername')

# Find submit button by ID and click
submit_button = driver.find_element(By.ID, 'submit')
submit_button.click()
💻

Example

This example opens a sample form page, fills in username and password fields, and submits the form by clicking the submit button.

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import time

# Setup Chrome options
options = Options()
options.add_argument('--headless')  # Run browser in headless mode

# Setup WebDriver service
service = Service()

# Initialize WebDriver
with webdriver.Chrome(service=service, options=options) as driver:
    # Open a demo form page
    driver.get('https://www.w3schools.com/html/html_forms.asp')

    # Fill in the "First name" field
    first_name = driver.find_element(By.ID, 'fname')
    first_name.clear()
    first_name.send_keys('John')

    # Fill in the "Last name" field
    last_name = driver.find_element(By.ID, 'lname')
    last_name.clear()
    last_name.send_keys('Doe')

    # Submit the form by clicking the submit button
    submit_button = driver.find_element(By.CSS_SELECTOR, 'input[type="submit"]')
    submit_button.click()

    # Wait to observe the result (not needed in real tests)
    time.sleep(2)

    # Print current URL to confirm submission
    print(driver.current_url)
Output
https://www.w3schools.com/html/html_forms.asp
⚠️

Common Pitfalls

  • Using incorrect locators causes NoSuchElementException. Always verify locators with browser DevTools.
  • Not clearing input fields before typing can append text instead of replacing it.
  • Trying to interact with elements before the page loads causes errors; use waits.
  • For buttons, ensure you use click() instead of send_keys().
python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wrong: typing without clearing
input_field = driver.find_element(By.ID, 'fname')
input_field.send_keys('John')  # May append if field not empty

# Right: clear before typing
input_field.clear()
input_field.send_keys('John')

# Wrong: interacting before element is ready
submit_button = driver.find_element(By.ID, 'submit')
submit_button.click()  # May fail if button not loaded

# Right: wait until clickable
wait = WebDriverWait(driver, 10)
submit_button = wait.until(EC.element_to_be_clickable((By.ID, 'submit')))
submit_button.click()
📊

Quick Reference

  • Locate element: driver.find_element(By.ID, 'id')
  • Type text: element.clear(); element.send_keys('text')
  • Click button: element.click()
  • Wait for element: Use WebDriverWait with expected conditions
  • Submit form: Click submit button or use element.submit()

Key Takeaways

Use reliable locators like ID, NAME, or CSS selectors to find form fields.
Always clear input fields before typing to avoid appending text.
Use explicit waits to ensure elements are ready before interacting.
Submit forms by clicking the submit button or calling submit() on a form element.
Test your locators and actions manually in the browser before automating.