Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
May 1, 2017

Handling Textboxes in Selenium





Handling textboxes in selenium will discuss about how to handle textboxes on a webpage. Basically a textbox is an input tag and will accept character sequence. While automating any application if you want to automate any textbox in selenium you have two types of options to enter data into the textbox. One is using sendKeys() method of selenium webdriver and another is using JavaScriptExecutor.

While entering into the data we need to remember some of the points:

  1. You need to use sendKeys() method to enter data into textbox after identifying the webelement on the webpage.
  2. If already a textbox contains data in it and you try to enter the data into same textbox then the data will be appended to the existing data.
  3. If you want to enter the data which is already having data in it. Then you need to clear the textbox before entering data into it.
  4. To clear the textbox we can use clear() method of selenium.
  5. Sometimes selenium methods will not work and not able to identify the elements and not able to enter data into it. Then we can take the help of JavaScriptExecutor to enter data into the textbox.





Now will see the examples for the above statements:

1. Use sendKeys() method to enter the data into textbox.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleTextbox {
    
    @Test
    public void textBox()
    {
        System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna");        
    }
}

2. Append data in a Textbox.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleTextbox {
    
    @Test
    public void textBox()
    {
        System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna");
        driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Sakinala");
    }
}





3. Clear the Textbox using clear() method.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleTextbox {
    
    @Test
    public void textBox()
    {
        System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).sendKeys("Krishna");
        driver.findElement(By.xpath("//*[@id='basicBootstrapForm']/div[1]/div[1]/input")).clear();
    }
}

4. JavaScriptExecutor to enter data into textbox.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class HandleTextbox {
    
    @Test
    public void textBox()
    {
        System.setProperty("webdriver.gecko.driver", "/KRISHNA VOLUME/drivers/geckodriver");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://demo.automationtesting.in/Register.html");
        JavascriptExecutor js = (JavascriptExecutor)driver;        
        js.executeScript("document.querySelector(\"input[placeholder='First Name']\").value='Krishna'");
    }
}

This way we can handle textboxes while automating the application using selenium webdriver.

Please watch YouTube video for better understanding.



Share this post: on Twitter on Facebook on Google+

JavaScriptExecutor in Selenium Katalon Analytics – Introduction

Related Posts

TAKING WEB ELEMENT SCREENSHOT IN SELENIUM

Selenium - Java

How to Capture WebElement Screenshot in Selenium Webdriver using selenium 4

avoid Switch to window

Selenium - Java

How to Avoid Switch To Window in Selenium WebDriver

webdrivermanager

Selenium - Java

No More Driver EXE files for Selenium Execution – Instead Use WebDriverManager

JAVASCRIPTEXECUTOR IN SELENIUM

Selenium - Java

JavaScriptExecutor in Selenium

ELEMENT NOT VISIBLE EXCEPTION

Selenium - C#, Selenium - Java

Selenium ElementNotVisibleException

NOSUCHWINDOW EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchWindowException

NOSUCHFRAME EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium NoSuchFrameException

NOSUCHELEMENT EXCEPTION

Selenium - C#, Selenium - Java

Selenium NoSuchElementException

SELENIUM WEBDRIVER EXCEPTIONS

Selenium - C#, Selenium - Java

Selenium WebDriver Exceptions

HANDLING BASIC ELEMENTS IN SELENIUM

Selenium - C#, Selenium - Java

Handling Basic Elements in Selenium

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023