sendKeys() method in Selenium Java

The sendKeys() method is used to type into an element. This post will discuss the sendKeys() method of the WebElement interface in detail.

  • Method declaration – void sendKeys(CharSequence… keysToSend)
  • What does it do? sendKeys() method is used to simulate typing into an element. This method is widely used in selenium to type into the input elements.

This function takes a CharSequence var-args into its arguments and then will type the same arguments into the element.

What is CharSequence? CharSequence is an interface in Java that represents a sequence of characters. This interface provides uniform, read-only access to various char sequences. Below is the list of classes that implement this interface –

  • CharBuffer
  • Segment
  • String
  • StringBuffer
  • StringBuilder

So, we can String, StringBuffer, or StringBuilder objects into the sendKeys() method, and the same will be entered into the element.

What are var-args? The var-args allow the method to accept zero or muliple arguments. So, we can pass zero or more arguments in the sendKeys() method.

Code Example

Now, let’s use the sendKeys() method. We will take a text input element and type “codekru” into that element using the sendKeys() method.

We will enter “codekru” in the below-highlighted element. You can find this element on our selenium playground website’s page – https://testkru.com/Elements/TextFields.

text element
  • We will first find our input element. We can use the findElement() method to find our element using the id attribute.
  • The id of our element is “firstName”.
  • And then, we can type the keyword “codekru” in that element by using the sendKeys("codekru") syntax.
public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstName"));

		// this will type "codekru" in the text field.
		element.sendKeys("codekru");
	}
}

The above code will type “codekru” in our input box.

The What If scenarios

What if we send multiple arguments in the sendKeys() method?

We will now send multiple arguments in the sendKeys method and will see what will be entered in our element.

We will send “codekru”, “website”, “for”, “learners” in the method.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstName"));
		element.sendKeys("codekru", "website", "for", "learners");
	}
}
Result after using sendKeys method

All keywords are entered in our input element one after the other. It will be like sending the “codekruwebsiteforlearners” in one go.

What if we use StringBuffer or StringBuilder instead of a String object in the sendKeys() method?

We can pass any class’s objects that implement the CharSequence interface. This includes StringBuffer and StringBuilder classes as well.

So, let’s pass the StringBuffer object in the sendKeys method and see whether the value is being typed in the element or not.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstName"));

		StringBuffer sb = new StringBuffer("A String buffer input");
		element.sendKeys(sb);
	}
}

The above code will type the StringBuilder text in the input element.

What if we don’t send arguments in the sendKeys() function?

As sendKeys method in selenium accepts var-args. So, it allows zero arguments as well, but we will get an IllegalArgumentException if we don’t send any argument.

public class CodekruTest {

	@Test
	public void test() {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
		WebDriver driver = new ChromeDriver();

		// opening the url
		driver.get("https://testkru.com/Elements/TextFields");

		WebElement element = driver.findElement(By.id("firstName"));

		// this will type "codekru" in the text field.
		element.sendKeys();
	}
}

Output –

java.lang.IllegalArgumentException: Keys to send should be a not null CharSequence
	at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:93)

Please visit this link to learn more about WebElement and its methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *