How to Handle Text Box in Selenium using java

              : Handling Textbox In Selenium:

1- How to Click on text box in selenium ?
To click on 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 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 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 that 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 text box using selenium web driver .


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();
 
       }
 }

sendkeys method in Selenium -sendkeys()

sendkeys is a method a predefined method that allow automation engineer to type or enter the value in a editable text field during the execution of 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 which is used to clear or reset an text box. clear in selenium is a predefine method of webdriver class.


Hope !!! Above tutorial of "handling text box in selenium " 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



Post a Comment

0 Comments