Filling Forms Using Selenium and Python
Web forms are a fundamental part of any website — whether it’s a login screen, contact page, or signup form. Automating form filling can be incredibly useful for testing, data entry, or scraping purposes. Selenium, a powerful browser automation tool, combined with Python, offers a seamless way to interact with form fields just like a human user would.
In this blog post, we’ll walk through how to fill out and submit forms using Selenium and Python, covering input fields, dropdowns, radio buttons, checkboxes, and more.
Prerequisites
Before we dive in, make sure you have the following installed:
- Python (3.x)
- Selenium (pip install selenium)
- A WebDriver (e.g., ChromeDriver for Google Chrome)
Here’s a basic setup:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome() # Ensure chromedriver is in your PATH
driver.get("https://example.com/form")
Filling Text Input Fields
To fill out text boxes like username, email, or passwords, locate the input field using find_element and use the send_keys() method:
python
username = driver.find_element(By.NAME, "username")
username.send_keys("testuser")
email = driver.find_element(By.ID, "email")
email.send_keys("user@example.com")
You can also clear an input field using clear():
python
username.clear()
username.send_keys("newuser")
Selecting Radio Buttons and Checkboxes
To select a radio button or checkbox, locate the element and use .click():
python
gender_male = driver.find_element(By.ID, "gender_male")
gender_male.click()
subscribe_checkbox = driver.find_element(By.NAME, "subscribe")
if not subscribe_checkbox.is_selected():
subscribe_checkbox.click()
Handling Dropdowns
For dropdown menus, use the Select class from selenium.webdriver.support.ui:
python
from selenium.webdriver.support.ui import Select
country_dropdown = Select(driver.find_element(By.ID, "country"))
country_dropdown.select_by_visible_text("United States")
# Or select by value
country_dropdown.select_by_value("US")
Submitting the Form
You can submit a form by clicking a button or using the submit() method:
python
submit_button = driver.find_element(By.XPATH, "//button[@type='submit']")
submit_button.click()
Alternatively, submit the form directly:
python
form = driver.find_element(By.TAG_NAME, "form")
form.submit()
Waiting for Elements
Web forms may take time to load. Use explicit waits to make your script more reliable:
python
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
username = wait.until(EC.presence_of_element_located((By.NAME, "username")))
Example: Full Form Automation
python
driver.get("https://example.com/signup")
driver.find_element(By.NAME, "username").send_keys("testuser")
driver.find_element(By.NAME, "email").send_keys("test@example.com")
driver.find_element(By.NAME, "password").send_keys("securepassword")
driver.find_element(By.ID, "agree_terms").click()
country = Select(driver.find_element(By.ID, "country"))
country.select_by_value("US")
driver.find_element(By.XPATH, "//button[text()='Sign Up']").click()
Conclusion
Automating form filling with Selenium and Python is straightforward and powerful. By combining element locators, input methods, and smart waiting strategies, you can interact with almost any form on the web. Whether you're testing UI flows, submitting data at scale, or scraping web portals, mastering form automation is an essential skill for any Selenium user.
With consistent practice, you can build robust automation scripts that handle complex form structures, validations, and dynamic behavior — all with the ease of Python.
Learn Selenium with Python Training Course
Read More: Link Text and Partial Link Text Locators in Selenium
Visit Quality Thought Training Institute in Hyderabad
Get Direction
Comments
Post a Comment