• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Selenium » Free Selenium Tutorial » Selenium Form WebElement: TextBox, Submit Button

Selenium Form WebElement: TextBox, Submit Button

Last Updated on: August 28, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Selenium Form WebElement
  • How to Handle Input Box
  • How you can enter data into Input Box
  • Deleting Values in Input Boxes
  • How to Deal with Buttons
  • Difference between Click()and Submit()

Selenium Form WebElement: In our post, we have seen how we can locate single and multiple elements with the help of Findelement and Findelements. Now in this post how we can access the various elements of a web form. As we know that for better looks of the online web pages we are using so that in a single form we can add so many various types of elements like Text boxes, Password fields, Checkboxes, Radio buttons, dropdowns, file inputs, etc.

So let’s know how we can deal with those elements with the help of Selenium WebDriver with Java. As in our previous post, we have seen WebDriver treat every element as an object of WebElement. So here we will see how to handle those elements and different available methods which helps in accessing those elements of a form.

Selenium Form WebElement

f you have not to take a look of our article regarding how you can able to locate elements on a web page with the help of Findelement and Findelements then we recommend to read the article first and after that read this article so that you can easily understand the how we have used those methods. If you have read that article then we can start with real-time examples of form elements like below:

  • How to Handle Input Box
  • How you Can enter Value in a Text Box
  • How you can Delete the Values of a Text Box
  • How to Deal With Buttons

How to Handle Input Box

We use Input Box to receiving the data from the outer worlds. There are two types of input boxes are present, that is:

  • Text Fields: This type of text boxes will accept the value and also display the value to the user
  • Password Text Box: This type of text boxes will accept the value but display those values in special characters to avoid displaying sensitive values.

To locate input box type elements you can use the Findelement methods with different locators like By.id(), By.name(), By.xpath(), By.CSSSelector() etc.

How you can enter data into Input Box

After locating the element uniquely we can use a special method that helps us to enter the specific data with the help of the sendkeys() method. Let’s see the below example how we can enter data into the text box

package com.selenium.practice.commomMethods;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SendKeysMethodEx 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/css-selector-adv-example.html");
      
      driver.findElement(By.id("username")).sendKeys("SoftwareTestingo");
      Thread.sleep(5000);
      driver.close();
   }
}

Deleting Values in Input Boxes

Similarly sendkeys() method there is a method present to clear the data in a text box. for that first we need to uniquely identify the elements after that we have to call the clear() method. This method does not require any parameters like the sendkeys() method. you can see the below example for code snippet:

package com.selenium.practice.commomMethods;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClearMethodEx 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/css-selector-adv-example.html");
      
      driver.findElement(By.id("username")).sendKeys("SoftwareTestingo");
      System.out.println("SoftwareTestingo Typed In Username Textbox");
      Thread.sleep(5000);
      driver.findElement(By.id("username")).clear();
      System.out.println("UserName Textbox Cleared");
      Thread.sleep(5000);
      driver.close();
   }
}

How to Deal with Buttons

For accessing the button we can take help of any of the below two methods

  • Click(): You can use this method to click a button on a web application. click() method will work on any of this elements type like type=”button” or type=”submit”.
package com.selenium.practice.commomMethods;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ClickEx 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/submit-click.html");
      
      driver.findElement(By.id("fname")).sendKeys("SoftwareTestingo");
      driver.findElement(By.id("lname")).sendKeys("Testing Blog");
      System.out.println("First Name & Last Name Filled");
      Thread.sleep(5000);
      driver.findElement(By.xpath("//button[contains(text(),'Reset')]")).click();
      System.out.println("Reset Button Clicked");
      Thread.sleep(5000);
      driver.close();
   }
}
  • Submit(): If you have look on the element tab of the console and if you have found the type=”submit” then, in that case, submit() method is a very good alternative of click method.
package com.selenium.practice.commomMethods;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SubmitEx 
{
   public static void main(String[] args) throws InterruptedException 
   {
      WebDriver driver;
      System.setProperty("webdriver.chrome.driver","Path Of Browser Driver");

      driver=new ChromeDriver();
      driver.manage().window().maximize();
      driver.get("https://softwaretestingo.blogspot.com/2020/08/submit-click.html");
      
      driver.findElement(By.id("fname")).sendKeys("SoftwareTestingo");
      driver.findElement(By.id("lname")).sendKeys("Testing Blog");
      System.out.println("First Name & Last Name Filled");
      Thread.sleep(5000);
      driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).submit();
      System.out.println("Submit Button Clicked");
      Thread.sleep(5000);
      driver.close();
   }
}

Note:

  • If any form button does not have the type=”submit” then the submit() will not be work.
  • If the button is not inside the <form> tag then also the submit() method will not work.

Difference between Click()and Submit()

Click() Submit()
You can use the .click() method to click on any button. There is no restriction for click buttons. we can use .submit() method for only submit the form after a click on the button.
 That means element’s type = “button” or type = “submit”, .click() method will works for both. That means element’s type = “submit” and the button should be inside <form> tag, then only submit() will work.
If button is inside <form> tag or button is outside <form> tag, the click() method will work. If element’s type = “button” means submit() will not work.
If button outside of the <form> tag means submit() will not work
When you perform Click operation on an element. then it causes a new page to load and discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException. If this current element is a form or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.

    Filed Under: Free Selenium Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers