Handling Text Box In Selenium Webdriver

.
To handle textbox in selenium we have sendkeys() and another method to perform an action according to need. Below list of all actions that are performed as needed.

How to click on a text box in the Selenium webdriver?

To click on the text box using Selenium just locate the web Element using any locators available eg. Xptah, CSS, ID etc.

Selenium Code :

//To Click on the text box in Selenium

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



How To Enter Value in TextBox Using Selenium Webdriver with Java?

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

Selenium Code 

//To enter or type value in text boxdriver.findElement(By.xpath("//input[@type='text']")).sendKeys("sandeep.uvw@gmail.com");


How To Clear the Text Box Value in Selenium Using Java?

clear()
To clear the textbox using Selenium Webdriver use the clear() method. clear() method in selenium is a predefined method of selenium web driver 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();


How To Get Entered/Typed Text from a Textbox in Selenium Using Java?

getAttribute()
To get the entered text from a textbox by using the selenium us getAttribute() method and also pass "value" as a 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);

              

How to Validate Whether Text is Entered or Not in the TextBox Using Selenium?

contains()
using contains() method we can validate /check whether text is 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 ");
              }


How to Check Whether a Text Field Is Blank or Not Using Selenium Webdriver?

Check the below code to validate whether 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");
              }


How To Verify The Placeholder Text Using Selenium Webdriver?

Check the below code to verify the placeholder of the text box using the Selenium web driver.

Selenium Code

 WebElement Wb= driver.findElement(By.xpath("//input[@type='text']"));
             
       //To check /verify the placeholder 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 Boxes in Selenium Webdriver?

In the Selenium web driver 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 boxes. Username Text Box and Password Text Box. 

Sendkeys Method in Selenium -sendkeys()

sendkeys () is a predefined method that allows automation engineers 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, class name, 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 on "Handling Text Box in Selenium "is helpful for you ...

Team,
QA Acharya

How to automate text box in Selenium

Post a Comment

0 Comments