How to get entered text from a textbox in selenium?

We can get the entered text from a textbox in Selenium webdriver. To obtain the value attribute of an element in the html document, we have to use the getAttribute() method. Then the value is passed as a parameter to the method.

Let us consider a textbox where we entered some text and then want to get the entered text.

If we spy on the element, we will find that there is no value attribute for this element in the html code.

After entering text inside that field, we can get the entered text by using the getAttribute() method.


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

public class GetValAttribute{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      String url = "";
      driver.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
      // identify element
      WebElement l = driver.findElement("gsc-i-id1"));
      // enter texts
      // get value attribute with getAttribute()
      String val = l.getAttribute("value");
      System.out.println("Entered text is: " + val);


Updated on: 18-Sep-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started