Wednesday, November 5, 2025

thumbnail

How to Handle Input Forms in Selenium

 ๐Ÿงพ Handling Input Forms in Selenium (Java)


Web forms allow users to enter data such as text, passwords, selections, and submit actions.

Using Selenium WebDriver, you can automate filling out and submitting these forms easily.


๐Ÿงฉ 1. Basic Steps to Handle Forms


Locate form elements (e.g., text boxes, buttons, dropdowns).


Enter data using Selenium methods like sendKeys().


Click buttons or submit the form using click() or submit().


Verify results (optional).


⚙️ 2. Commonly Used Methods

Method Description

findElement(By by) Finds an element on the page.

sendKeys(String text) Enters text into an input field.

click() Clicks a button, checkbox, or link.

clear() Clears existing text from an input box.

submit() Submits a form.

๐Ÿง  3. Example – Filling Out a Simple Login Form


Imagine a login form like this:


<form id="loginForm">

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

  <input type="password" id="password" name="password" />

  <button type="submit" id="loginButton">Login</button>

</form>



Here’s how to automate it using Selenium:


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class FormExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();


        // Open a website with a form

        driver.get("https://example.com/login");


        // Locate input fields and buttons

        WebElement usernameField = driver.findElement(By.id("username"));

        WebElement passwordField = driver.findElement(By.id("password"));

        WebElement loginButton = driver.findElement(By.id("loginButton"));


        // Interact with elements

        usernameField.clear();

        usernameField.sendKeys("myUsername");


        passwordField.clear();

        passwordField.sendKeys("myPassword");


        // Click login button

        loginButton.click();


        // Print page title after login

        System.out.println("Page Title: " + driver.getTitle());


        driver.quit();

    }

}


๐Ÿ’ฌ 4. Locating Elements


You can locate form elements using various locators:


Locator Type Example Description

By.id() By.id("username") Locates element by its ID attribute.

By.name() By.name("email") Locates element by its name attribute.

By.className() By.className("input-field") Locates by CSS class name.

By.xpath() By.xpath("//input[@type='text']") Locates by XPath expression.

By.cssSelector() By.cssSelector("input#username") Locates using CSS selector.

๐Ÿงฎ 5. Handling Other Form Elements

✅ Checkboxes:

WebElement checkbox = driver.findElement(By.id("rememberMe"));

if (!checkbox.isSelected()) {

    checkbox.click();

}


๐Ÿ”˜ Radio Buttons:

WebElement genderMale = driver.findElement(By.id("male"));

genderMale.click();


๐Ÿ“‹ Dropdowns:


Use Selenium’s Select class.


import org.openqa.selenium.support.ui.Select;


Select country = new Select(driver.findElement(By.id("country")));

country.selectByVisibleText("India");


๐Ÿ“ค Submit Forms:

WebElement form = driver.findElement(By.id("loginForm"));

form.submit(); // Alternative to clicking a submit button


๐Ÿงฉ 6. Validation After Submission


You can check if form submission was successful by:


Checking the page title


Checking confirmation messages


Verifying URL change


Example:


String successMessage = driver.findElement(By.id("successMsg")).getText();

System.out.println("Message: " + successMessage);


๐Ÿ› ️ 7. Best Practices


✅ Use explicit waits (WebDriverWait) for dynamic elements.

✅ Always clear input fields before entering data.

✅ Handle alerts or pop-ups if they appear after submission.

✅ Use Page Object Model (POM) for cleaner, reusable code.


๐Ÿ Summary

Task Selenium Method

Enter text sendKeys()

Clear text clear()

Click button click()

Select dropdown Select class

Submit form submit()

Verify output getText() / getTitle()

Learn Selenium with JAVA Training in Hyderabad

Read More

Working with ChromeDriver, GeckoDriver, and EdgeDriver

Introduction to WebDriver Interface in Java

Difference Between Selenium RC, IDE, and WebDriver

How to Install and Configure Eclipse for Selenium Testing

Visit Our Quality Thought Institute in Hyderabad

Get Directions


Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive