Automating Form Filling Using Selenium and Python

๐Ÿค– Automating Form Filling Using Selenium and Python (English)

Selenium is a powerful tool for automating web browsers. With Python, you can use Selenium to automatically fill out and submit forms on websites—useful for testing, data entry, scraping, and automation tasks.


✅ 1. Setup: Install Selenium

bash

Copy

Edit

pip install selenium

Also, download a WebDriver for your browser (e.g., ChromeDriver for Chrome):

๐Ÿ‘‰ https://chromedriver.chromium.org/downloads


Place the driver in your system PATH or in your project folder.


๐Ÿงฑ 2. Basic Form Automation Example

Suppose you want to fill out a simple form with name, email, and message.


๐Ÿงช Example Form HTML

html

Copy

Edit

<form id="contact-form">

  <input type="text" name="name" />

  <input type="email" name="email" />

  <textarea name="message"></textarea>

  <button type="submit">Send</button>

</form>

๐Ÿ Python Code Using Selenium

python

Copy

Edit

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.common.keys import Keys

import time


# Set up the browser (Chrome in this case)

driver = webdriver.Chrome()


# Go to the form page

driver.get("https://example.com/contact")


# Fill in the fields

driver.find_element(By.NAME, "name").send_keys("John Doe")

driver.find_element(By.NAME, "email").send_keys("john@example.com")

driver.find_element(By.NAME, "message").send_keys("Hello, this is a test message.")


# Optionally wait or scroll

time.sleep(1)


# Submit the form

driver.find_element(By.XPATH, "//button[@type='submit']").click()


# Wait and close browser

time.sleep(3)

driver.quit()

๐Ÿง  3. Common Ways to Locate Elements

Locator Method Example

By.ID By.ID, "username"

By.NAME By.NAME, "email"

By.CLASS_NAME By.CLASS_NAME, "form-input"

By.TAG_NAME By.TAG_NAME, "input"

By.XPATH By.XPATH, "//input[@name='name']"

By.CSS_SELECTOR By.CSS_SELECTOR, "input[name='email']"


⚠️ 4. Tips for Reliable Automation

Use time.sleep() or WebDriverWait to wait for elements to load.


Always handle exceptions using try/except to catch issues like missing elements.


Headless mode (for running without a UI):


python

Copy

Edit

from selenium.webdriver.chrome.options import Options

options = Options()

options.add_argument("--headless")

driver = webdriver.Chrome(options=options)

๐Ÿ” 5. Security Note

Don’t use Selenium to automate login or fill secure forms (like banking or CAPTCHAs) unless it's for testing your own application.


๐Ÿงช Optional: Wait for Elements

Use explicit waits for better reliability than time.sleep():


python

Copy

Edit

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC


wait = WebDriverWait(driver, 10)

name_input = wait.until(EC.presence_of_element_located((By.NAME, "name")))

๐Ÿ Summary

Step Description

Install Selenium pip install selenium

Set up driver ChromeDriver/FirefoxDriver

Locate fields Use By.NAME, By.ID, By.XPATH, etc.

Fill and submit send_keys(), .click()

Wait & cleanup time.sleep() or WebDriverWait + driver.quit()

Learn Selenium Python Training in Hyderabad

Read More

Handling Alerts, Pop-ups, and iFrames in Selenium with Python

Handling Input Boxes, Buttons, and Checkboxes in Selenium

Visit Our Quality Thought Training in Hyderabad

Get Directions

Comments

Popular posts from this blog

Understanding Snowflake Editions: Standard, Enterprise, Business Critical

Why Data Science Course?

How To Do Medical Coding Course?