How to type in textbox using Selenium WebDriver with Java?


We can type in a textbox using Selenium webdriver. We shall use the sendKeys() method to type in the edit box. It is an in-built method in Selenium. Let us consider a text box where we shall enter some text.

First of all, we would identify the field with one of the locators and apply sendKeys() method on it.

Syntax −

driver.findElement(By.id("text-bx")).sendKeys("Tutorialspoint")

Example

Code Implementation.

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 InputTxt{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver",
      "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      // identify element and input text inside it
      WebElement l =driver.findElement(By.className("gsc-input"));
      l.sendKeys("Selenium");
      driver.quit();
   }
}

Output

Updated on: 28-Aug-2020

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements