sendKeys() method to type the text in the input field

We can type the text in the input field by using sendKeys() method.  The sendKeys will simulate the keyboard and it will type the into the text area input field.

For example, If the user wants to enter the user id and password in the input field facebook or gmail login page, this method helps to type the inputs in the text area.


JAVA API Syntax : void sendKeys(java.lang.CharSequence...keysToSend)


Charsequence will enter the key inputs from the keyboard.  This does not return any values.


Even we can perform the special keys such as ENTER, TAB, SHIFT, BACKSPACE etc., We can type anything from the keyboard which we want.


Code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class sendkeys {
 
public static void main(String args[]) {
  
//Launching the firefoxbrowser
WebDriver driver = new FirefoxDriver();
    
//Implicit wait, this method is used to wait for the webelement to load  
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
    
//Navigate to FB page
driver.get("http://www.facebook.com");
    
//typing the texts using sendkeys method for user id and password
driver.findElement(By.id("email")).sendKeys("xxxxx@gmail.com");   
driver.findElement(By.id("pass")).sendKeys("xxxxx@123");  
driver.findElement(By.id("loginbutton")).click(); 
     
//Navigate to google search page
driver.navigate().to("https://www.google.com");
     
//Enter the text using SHIFT keys in search box
WebElement gsearch = driver.findElement(By.name("q"));
gsearch.sendKeys(Keys.chord(Keys.SHIFT,"www.qafreaks.com"));
  
//To click the search button
driver.findElement(By.name("btnG")).click();
}
}
Socialize It and Share the post with your friends
SOCIALIZE IT →
FOLLOW US →
SHARE IT →