How to enter value in Textfield/ Textarea

This is the first and foremost command that a beginner uses while learning Selenium WebDriver.

So, lets have a look at one of the most used command of WebDriver API.

Method definition :

void sendKeys(String inputValue) : The method enters text passed as inputValue string in a textbox/ textarea. The method does not return any value.

The sendKeys() method is defined in WebElement interface, and the same is implemented in RemoteWebElement class which implements WebElement interface.

Lets have a look at simple code to simulate type event using sendKeys() method using Selenium WebDriver.

public class SeleniumDemo {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http://www.seleniumlive.wordpress.com/”);

// locating a searchBar using xpath
WebElement searchBar = driver.findElement(By.xpath(“//input[@class=’search-field’]”));

// calling sendKeys method on located WebElement
searchBar.sendKeys(“JavascriptExecutor”);

}

}
To see the advanced usage of sendKeys() visit the link.

Hope it helps you!

Cheers 🙂

Advertisements

#enter-text-in-text-box, #sendkeys, #webdriver