Typing text (sendKeys) in Selenium Java - Build an Automation Script
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.time.Duration; public class TypingTextTest { private WebDriver driver; private WebDriverWait wait; @BeforeClass public void setUp() { // Set path to chromedriver if needed driver = new ChromeDriver(); wait = new WebDriverWait(driver, Duration.ofSeconds(10)); driver.get("https://example.com"); // Replace with actual URL } @Test public void testTypingTextInSearchInput() { // Wait until the search input is visible and enabled WebElement searchInput = wait.until(ExpectedConditions.elementToBeClickable(By.id("search-input"))); // Click to focus searchInput.click(); // Type text String textToType = "Selenium testing"; searchInput.sendKeys(textToType); // Verify the input contains the typed text String enteredText = searchInput.getAttribute("value"); Assert.assertEquals(enteredText, textToType, "The search input should contain the typed text."); } @AfterClass public void tearDown() { if (driver != null) { driver.quit(); } } }
This test script uses Selenium WebDriver with Java and TestNG for assertions.
Setup: We open the browser and navigate to the homepage URL.
Wait: We use an explicit wait to ensure the search input field with id 'search-input' is clickable (visible and enabled).
Action: We click the input to focus and then type the text 'Selenium testing' using sendKeys.
Assertion: We get the value attribute of the input and assert it matches the typed text exactly.
Teardown: We close the browser after the test.
This approach ensures the element is ready before typing and verifies the input content correctly.
Now add data-driven testing with 3 different input texts to verify typing works for each.
Practice
sendKeys() method do in Selenium WebDriver?Solution
Step 1: Understand the purpose of sendKeys()
ThesendKeys()method simulates typing text into input fields.Step 2: Compare with other actions
Clicking buttons, navigating URLs, and taking screenshots are done by other methods, notsendKeys().Final Answer:
It types text into an input field on a web page. -> Option BQuick Check:
sendKeys() = types text [OK]
- Confusing sendKeys() with click()
- Thinking sendKeys() navigates pages
- Assuming sendKeys() takes screenshots
Solution
Step 1: Locate element and use sendKeys()
The correct way is to find the element first, then callsendKeys()on it with the text.Step 2: Check syntax correctness
driver.findElement(By.id("inputField")).sendKeys("hello"); usesfindElementandsendKeyscorrectly. Others use invalid methods or wrong parameters.Final Answer:
driver.findElement(By.id("inputField")).sendKeys("hello"); -> Option AQuick Check:
findElement + sendKeys() = correct syntax [OK]
- Calling sendKeys() directly on driver
- Using non-existent methods like type()
- Passing text to click() method
WebElement input = driver.findElement(By.name("username"));
input.sendKeys("user123");
String value = input.getAttribute("value");
System.out.println(value);Solution
Step 1: Understand sendKeys() effect
ThesendKeys("user123")types "user123" into the input field.Step 2: Retrieve input value attribute
ThegetAttribute("value")returns the current text inside the input, which is "user123".Final Answer:
It prints: user123 -> Option DQuick Check:
sendKeys() sets value, getAttribute("value") reads it [OK]
- Expecting getAttribute("value") to return element name
- Assuming sendKeys() throws exception without element
- Thinking printed value is empty
WebElement password = driver.findElement(By.id("pass"));
password.sendkeys("admin");Solution
Step 1: Check method spelling
The method name is case-sensitive and must besendKeyswith uppercase 'K'.Step 2: Verify locator and usage
The locatorBy.id("pass")is valid and sendKeys works on password fields. Driver initialization is assumed done.Final Answer:
Method name should be sendKeys with uppercase K. -> Option AQuick Check:
sendKeys() method name is case-sensitive [OK]
- Typing sendkeys() with lowercase k
- Assuming sendKeys() can't type passwords
- Ignoring driver setup errors
Solution
Step 1: Clear existing text properly
Theclear()method empties the input field safely before typing.Step 2: Type new text after clearing
After clearing, callingsendKeys("hello world")types the new text correctly.Final Answer:
driver.findElement(By.name("search")).clear(); driver.findElement(By.name("search")).sendKeys("hello world"); -> Option CQuick Check:
Use clear() before sendKeys() to reset input [OK]
- Trying to delete text with sendKeys() shortcuts incorrectly
- Sending empty string to clear input
- Not clearing input before typing new text
Ready to prove it?
Pass the test and this pattern moves unknown
