How to Handle Text Box in Selenium using Java

              : Handling Textbox In Selenium:

1- How to Click on a text box in selenium?
To click on the text box using selenium just locate the web Element using locators.

Selenium Code :

//To click on text box in selenium

driver.findElement(By.xpath("//input[@type='text']"));



2- How to enter value in text box in selenium webdriver java ?

Sendkey()- To enter / type value in text box using selenium wbedriver use sendkeys() method .sendkeys() method in selenium is used to enter value in textbox during the execution of test.

Selenium Code :

//To enter or type value in text box

driver.findElement(By.xpath("//input[@type='text']")).sendKeys("sandeep.uvw@gmail.com");


3-How to clear the text box value in selenium?

clear()- To clear the textbox using selenium webdriver use clear() method . clear() method in selenium is a predefined method of selenium webdriver class used to clear the entered text or displayed in the textbox.

Selenium Code :

//To Clear the text Box

driver.findElement(By.xpath("//input[@type='text']")).clear();


4-How to get entered / typed text from a textbox in selenium ?

getAttribute()- To get the entered text from a textbox by using selenium us getAttribute() method and also pass "value" as parameter to the method.


Selenium Code :

  //To Get the entered text in selenium

 WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));

 Wb.sendKeys("sandeep.uvw@gmail.com");

String txt= Wb.getAttribute("value");

System.out.println(txt);

              

5-How to validate text entered or not in textbox using selenium?

using contains() method we can validate /check that text entered or not in the text box.


Selenium Code :

 WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
 Wb.sendKeys("sandeep.uvw@gmail.com");
 String txt= Wb.getAttribute("value");
 System.out.println(txt);
             
              //TO validate text entered or not in textbox
              if  (txt.contains("sandeep.uvw@gmail.com"))
              {
                     System.out.println("Yes text is entered ");
              }
              else
              {
                     System.out.println("Text is not entered ");
              }


6-How to check whether a text field is blank or not using selenium webdriver?

Check the below code to validate the text box is empty or not.


Selenium Code :

   WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
             
              //To Check that text box is empty or not
              String CLbox =Wb.getAttribute("value");
              if(CLbox.isEmpty())
              {
                     System.out.println(" Yes Text box is clear");
              }
              else
              {
                     System.out.println("No Text box is not clear");
              }

7-How to verify the placeholder text using selenium webdriver?

Check the below code to verify the placeholder of the text box using the selenium webdriver.


Selenium Code:

   WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
             
       //To check /verify the place holder in selenium
       String plcaehld=Wb.getAttribute("placeholder");
       if(plcaehld.equals("Email address or phone number"))
{
       System.out.println("Valid Place holder");
}
       else
       {
              System.out.println("invalid place holder");
       }

Complete Selenium Code to handle a textbox 

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class TextBox {
 
   public static void main(String[] args) {
   // TODO Auto-generated method stub
  System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver.exe");
  WebDriver driver=new ChromeDriver();
             
   //Maximize Browser Window
    driver.manage().window().maximize();
             
   //Enter URL in Browser
  driver.get("https://www.facebook.com//");
   driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
           
   //To enter the value in text box
 driver.findElement(By.xpath("//input[@type='text']")).sendKeys("sandeep.uvw@gmail.com");
             
   //To Clear the text Box
   driver.findElement(By.xpath("//input[@type='text']")).clear();
             
   //To Get the enterd text in selenium
   WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));

  //To check /verify the placeholder text in selenium
       String plcaehld=Wb.getAttribute("placeholder");
       if(plcaehld.equals("Email address or phone number"))
{
       System.out.println("Valid Place holder");
}
       else
       {
              System.out.println("invalid place holder");
       }
   
 //To Check that text box is empty or not
              String CLbox =Wb.getAttribute("value");
              if(CLbox.isEmpty())
              {
                     System.out.println(" Yes Text box is clear");
              }
              else
              {
                     System.out.println("No Text box is not clear");
              }

   Wb.sendKeys("sandeep.uvw@gmail.com");

//TO validate text entered or not in textbox
    String txt= Wb.getAttribute("value");
    System.out.println(txt);
  
              if  (txt.contains("sandeep.uvw@gmail.com"))
              {
                     System.out.println("Yes text is entered ");
              }
              else
              {
                     System.out.println("Text is not entered ");
              }
             
    //Close current browser window
     driver.close();
 
       }
 }

how to Handle Multiple Text Box in Selenium Webdriver?
In Selenium webdriver to handle multiple textboxes using java just Follow the below steps. 

Selenium Code :
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
 
import org.openqa.selenium.chrome.ChromeDriver;
 
public class FacebookLogin {
 
     public static void main(String[] args) {
           // TODO Auto-generated method stub
           System.setProperty("webdriver.chrome.driver", "C:\\Software\\chromedriver.exe");
           WebDriver driver=new ChromeDriver();
           driver.get("https://www.facebook.com");
           //Handling Multiple text Box in Selenium
         //This is First Textbox
           driver.findElement(By.xpath("//input[ @type=\"text\"]")).sendKeys("TextBox1");
           //This is Second Textbox
     driver.findElement(By.xpath("//input[@type=\"password\"]")).sendKeys("TextBox2");
 
}
}

The Above selenium code will handle multiple text box ie. Username Text Box and Password Text box. 

sendkeys method in Selenium -sendkeys()

sendkeys is a predefined method that allows automation engineer to type or enter the value in an editable text field during the execution of the test. These fields are identified by selenium locators- name,classname , id, Xpath,etc.


Clear method in Selenium - clear()

clear is a predefined method in selenium that is used to clear or reset a text box. clear in selenium is a predefined method of web driver class.


Hope !!! The above tutorial of "handling text box in selenium "is helpful for you ...

Team,
QA Acharya

Tags : Textbox in selenium , sendkeys selenium , clear method in selenium , to verify the placeholder text in selenium 



How to automate text box in selenium



Admin-Mumbai office

Post a Comment

Previous Post Next Post