• Selenium Video Tutorials

Selenium Webdriver - Input Boxes



Selenium Webdriver can be used to handle input boxes (also referred to as a text box). In HTML terminology, every input box is identified by the tagname called input.

Let us now discuss the identification of an input box on a webpage shown in the below image. First, we would need to right click on the webpage, and then click on the Inspect button in the Chrome browser. Then, the corresponding HTML code for the whole page would be visible. For investigating a single element on a page, we would need to click on the left upward arrow, available to the top of the visible HTML code as highlighted below.

Selenium Input Boxes 1

Once we had clicked and pointed the arrow to the input box (highlighted in the below image), its HTML code was visible, both reflecting the input tagname (enclosed in <>).

<input id="fullname" name="fullname" type="text" class="form-control" placeholder="Full Name">
Selenium Input Boxes 2

Let us take an example of the above page, where we would first enter some text in the input box with the help of the sendKeys() method. The value to enter is passed as an argument to the method. Then, we would wipe out the text entered with the clear() method.

Syntax

Webdriver driver = new ChromeDriver();
driver.findElement(By.xpath("value of xpath")).sendKeys("value entered"); 
driver.findElement(By.xpath("value of xpath")).clear();

Example

Code Implementation on HandlingInputBox.java class file.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class HandlingInputBox {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // Opening the webpage where we will identify edit box and enter
      driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // enter text in input box
      e.sendKeys("Selenium");

      // Get the value entered
      String text = driver.findElement
         (By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Entered text is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = driver.findElement
         (By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

      // Closing browser
      driver.quit();
   }
}

Output

Entered text is: Selenium
Get text after clearing: 

Process finished with exit code 0

In the above example, we had first entered the text Selenium in the input box, and also retrieved the value entered in the console with the message- Entered text is: Selenium. Then cleared the value entered and got no value in the input box after clearing up the text. Hence, we had also received the message in the console: Get text after clearing:.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

We can also use the sendKeys() method along with the Actions class in Selenium. We would take the same example discussed above and see the implementation.

Syntax

Syntax to enter text in the input box using the Actions class.

Webdriver driver = new ChromeDriver();
WebElement e = driver.findElement(By.xpath("value of xpath”));
Actions actions = new Actions(driver);
actions.sendKeys(e, "value entered").perform();

Example

Code Implementation on HandlingInputBoxWithActions.java class file.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;

public class HandlingInputBoxWithActions {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

      // Opening the webpage where we will identify edit box to enter 
      driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // enter text in input box using Actions class
      Actions action = new Actions(driver);
      action.sendKeys(e, "SeleniumS").perform();

      // Get the value entered
      String text = driver.findElement
         (By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Entered text with Actions class is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = driver.findElement
         (By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

      // Closing browser
      driver.quit();
   }
}

Output

Entered text with Actions class is: SeleniumS
Get text after clearing:

Process finished with exit code 0

In the above example, we had first entered the text SeleniumS in the input box, and also retrieved the value entered in the console with the message- Entered text with Actions class is: SeleniumS. Then cleared the value entered and got no value in the input box after clearing up the text. Hence, we had also received the message in the console: Get text after clearing:.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

We can also use the JavaScriptExecutor to input text in the input box in Selenium. We would take the same example discussed above and see the implementation.

Syntax

Syntax to input text in input box.

WebElement e = driver.findElement(By.xpath("value of xpath"));
JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
javascriptExecutor.executeScript("arguments[0].setAttribute('value', 'Selenium Java')", e);

Example

Code Implementation on HandlingInputBoxWithJS.java class file.

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class HandlingInputBoxWithJS {
   public static void main(String[] args) throws InterruptedException {
      
      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 15 secs
      driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
      
      // Opening the webpage where we will identify edit box to enter
      driver.get("https://www.tutorialspoint.com/selenium/practice/text-box.php");

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // enter text in input box using JavascriptExecutor
      JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
      javascriptExecutor.executeScript("arguments[0].setAttribute('value', 'Selenium Java')", e);

      // Get the value entered
      String text = driver.findElement(By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Entered text with JavaScript Executor is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = driver.findElement(By.xpath("//*[@id='fullname']")).getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

      // Closing browser
      driver.quit();
   }
}

Output

Entered text with JavaScript Executor is: Selenium Java
Get text after clearing: 

Process finished with exit code 0

In the above example, we had first entered the text Selenium Java in the input box, and also retrieved the value entered in the console with the message - Entered text with JavaScript Executor is: Selenium Java. Then cleared the value entered and got no value in the input box after clearing up the text. Hence, we had also received the message in the console: Get text after clearing:.

Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.

Thus, in this tutorial, we had discussed how to handle input boxes using the Selenium Webdriver.

Advertisements