Typing text into input fields is how you simulate a user entering information on a webpage. It helps test if forms and search boxes work correctly.
Typing text (sendKeys) in Selenium Java
Introduction
Syntax
Selenium Java
WebElement element = driver.findElement(By.id("elementId")); element.sendKeys("text to type");
Use sendKeys() on a WebElement to type text into input fields.
Make sure to locate the element correctly before typing.
Examples
Selenium Java
driver.findElement(By.name("username")).sendKeys("myUser123");
Selenium Java
WebElement searchBox = driver.findElement(By.cssSelector("input.search")); searchBox.sendKeys("Selenium testing");
Selenium Java
driver.findElement(By.xpath("//input[@placeholder='Email']")).sendKeys("test@example.com");
Sample Program
This test opens a login page, types 'testUser' into the username field and 'pass123' into the password field, then prints a success message.
Selenium Java
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class TypingTextTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/login"); WebElement usernameField = driver.findElement(By.id("username")); usernameField.sendKeys("testUser"); WebElement passwordField = driver.findElement(By.id("password")); passwordField.sendKeys("pass123"); System.out.println("Text typed successfully."); } finally { driver.quit(); } } }
Important Notes
Always wait for the element to be visible before typing to avoid errors.
Use clear() method if you want to erase existing text before typing new text.
Summary
sendKeys() types text into input fields in Selenium.
Locate the element first, then call sendKeys() with the text.
This simulates user typing and helps test form inputs.
Practice
1. What does the
sendKeys() method do in Selenium WebDriver?easy
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]
Hint: sendKeys() = typing text into fields [OK]
Common Mistakes:
- Confusing sendKeys() with click()
- Thinking sendKeys() navigates pages
- Assuming sendKeys() takes screenshots
2. Which of the following is the correct syntax to type "hello" into a text input using Selenium in Java?
easy
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]
Hint: Always find element first, then call sendKeys() [OK]
Common Mistakes:
- Calling sendKeys() directly on driver
- Using non-existent methods like type()
- Passing text to click() method
3. What will be the result of the following code snippet?
WebElement input = driver.findElement(By.name("username"));
input.sendKeys("user123");
String value = input.getAttribute("value");
System.out.println(value);medium
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]
Hint: sendKeys() sets input value, getAttribute("value") reads it [OK]
Common Mistakes:
- Expecting getAttribute("value") to return element name
- Assuming sendKeys() throws exception without element
- Thinking printed value is empty
4. Identify the error in the following code snippet that tries to type "admin" into a password field:
WebElement password = driver.findElement(By.id("pass"));
password.sendkeys("admin");medium
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]
Hint: Remember exact method name: sendKeys() with capital K [OK]
Common Mistakes:
- Typing sendkeys() with lowercase k
- Assuming sendKeys() can't type passwords
- Ignoring driver setup errors
5. You want to clear any existing text and then type "hello world" into a search box using Selenium in Java. Which code snippet correctly does this?
hard
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]
Hint: Use clear() before sendKeys() to replace text [OK]
Common Mistakes:
- Trying to delete text with sendKeys() shortcuts incorrectly
- Sending empty string to clear input
- Not clearing input before typing new text
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
