Chapter 6_1: Form elements Textbox and submit button

Forms are the important web elements to receive information from the website visitors. Web forms have different GUI elements like Text boxes, Password fields, Check-boxes, Radio buttons, drop-downs, file inputs, etc.

We will see how to access these different form elements using Selenium Web Driver with Java. Selenium encapsulates every form element as an object of WebElement. It provides API to find the elements and take action on them like entering text into text boxes, clicking the buttons, etc. We will see the methods that are available to access each form 

Input Box

Input boxes refer to either of these two types:

  1. Text Fields– text boxes that accept typed values and show them as they are.
  2. Password Fields– text boxes that accept typed values but mask them as a series of special characters (commonly dots and asterisks) to avoid sensitive values to be displayed.

Entering Values in Input Boxes

To enter text into the Text Fields and Password Fields, sendKeys() is the method available on the WebElement.

Using the same example of http://newtours.demoaut.com/site, here is how we find the Text field and Password fields and enter values into them.

 // get the webelement corresponding to username(Textfield) 
 WebElement name=driver.findElement(By.name("userName")); 
		  name.sendKeys("Testology");
// get the webelement corresponding to password(passwordfield) 
  WebElement password=driver.findElement(By.name("password"));
		  password.sendKeys("Testology12"); 

Deleting Values in Input Boxes

The clear() method is used to delete the text in an input box. This method does not need a parameter. The code snippet below will clear out the text from the Email or Password fields

name.clear(); 
 password.clear();

Buttons

The buttons can be identify using the click() method.

In the following example

  1. Find the button to Sign in
  2. Click on the “Sign-in” Button in the login page of the site to login to the site.
WebElement submitbutton=driver.findElement(By.cssSelector
("input[name='login'][value='Login']"));
 submitbutton.click();

Complete Code

Here is the complete working code:

package testology;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class forms 
{
    public static void main(String[] args) 
    {
System.setProperty("webdriver.driver.chromedriver", "chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().window().maximize();
        driver.get("http://newtours.demoaut.com/");
        driver.findElement(By.linkText("SIGN-ON")).click(); 
 // get the webelement corresponding to username(Textfield) 
 WebElement name=driver.findElement(By.name("userName")); 
  name.sendKeys("Testology");
 // get the webelement corresponding to password (passwordfield)    
 WebElement password=driver.findElement(By.name("password"));
		  password.sendKeys("Testology12"); 
//to deleting the values in the textbox
		  name.clear(); 
		  password.clear();
 WebElement submitbutton=driver.findElement(By.cssSelector(
		  "input[name='login'][value='Login']"));
		  submitbutton.click();
      }
  }