Submitting forms lets you send data on a webpage to the server. It helps test if the form works correctly.
0
0
Submitting forms in Selenium Python
Introduction
When testing a login page to check if user credentials are accepted.
When verifying a signup form submits user details properly.
When checking if a search form returns correct results after submission.
When testing contact forms to ensure messages are sent.
When automating checkout processes in online shopping sites.
Syntax
Selenium Python
form_element.submit()
You first find the form element using a locator.
Calling submit() sends the form data as if you clicked the submit button.
Examples
Finds a form by its ID and submits it.
Selenium Python
form = driver.find_element(By.ID, "loginForm")
form.submit()Finds a form by its name attribute and submits it directly.
Selenium Python
driver.find_element(By.NAME, "searchForm").submit()Submits the form by calling submit on an input inside the form.
Selenium Python
search_box = driver.find_element(By.ID, "searchInput") search_box.send_keys("Selenium") search_box.submit()
Sample Program
This script opens a login page, fills in username and password, submits the form, and checks if login succeeded by URL.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By import time # Setup driver (make sure chromedriver is in PATH) driver = webdriver.Chrome() # Open a simple login page driver.get("https://example.com/login") # Find username and password fields username = driver.find_element(By.ID, "username") password = driver.find_element(By.ID, "password") # Enter credentials username.send_keys("testuser") password.send_keys("mypassword") # Find the form and submit it login_form = driver.find_element(By.ID, "loginForm") login_form.submit() # Wait to see result time.sleep(3) # Check if login was successful by URL change or element presence if "dashboard" in driver.current_url: print("Login successful") else: print("Login failed") # Close browser driver.quit()
OutputSuccess
Important Notes
Calling submit() on any element inside the form works, not just the form itself.
Make sure the form element is correctly located to avoid errors.
Sometimes clicking the submit button is preferred if the form has JavaScript handling submission.
Summary
Use submit() on a form element to send form data.
You can submit by locating the form or an input inside it.
Submitting forms helps test user input and server response.