Typing and Clearing Text Inputs
sendKeys(CharSequence...) simulates real keystrokes into a focused input element, firing the same keydown/keypress/keyup and input events a physical keyboard would, which is important because many forms rely on JavaScript input listeners to validate as you type. clear() resets a text field to empty by selecting all and deleting, and should almost always be called before sendKeys() on a field that might already contain a value (such as a pre-filled default or leftover text from a previous test run), because sendKeys() appends to existing content rather than replacing it.
Cricket analogy: Calling clear() before sendKeys() is like wiping the crease markings before a new over so the umpire isn't confused by old guard marks, ensuring the batsman's stance data starts fresh rather than appending to stale markings.
Submitting Forms
WebElement.submit() locates the enclosing <form> and submits it directly, equivalent to pressing Enter in a text field, but it bypasses any onclick JavaScript handler attached specifically to a submit button. Clicking the actual submit button with click() is closer to real user behavior and will fire any attached click handlers, such as client-side validation or analytics tracking. To read back a filled-in value for verification, use getAttribute("value") rather than getText(), since getText() only returns rendered visible text and input elements store their content in the value attribute, not as child text nodes.
Cricket analogy: Calling form.submit() is like a captain declaring the innings directly without needing to physically signal the umpire, whereas clicking a submit button is like walking up and formally notifying the umpire — both end the innings but through different interaction paths, and only the formal signal triggers the ground announcement (an onclick handler).
File Uploads
For <input type="file"> elements, you don't need to interact with the native OS file-picker dialog at all — that dialog is outside the browser's automatable DOM and Selenium can't click into it. Instead, call sendKeys() on the file input and pass the absolute file path as a string; the browser accepts this directly as if the user had picked that file, without ever opening the picker UI. This only works if the input element is present in the DOM (not hidden via display:none in a way some browsers reject) and is a genuine native file input rather than a custom-styled upload widget layered on top of one.
Cricket analogy: Uploading a file via sendKeys() with an absolute path is like handing the umpire a pre-prepared team sheet rather than dictating eleven names one by one — Selenium hands off the whole file path in one shot rather than interacting with any picker dialog.
WebElement username = driver.findElement(By.id("username"));
username.clear();
username.sendKeys("selenium_user");
WebElement password = driver.findElement(By.id("password"));
password.clear();
password.sendKeys("S3cure!Pass");
// Verify the typed value using getAttribute, not getText
String typedValue = username.getAttribute("value");
Assert.assertEquals(typedValue, "selenium_user");
// Click the real submit button so onclick handlers fire
driver.findElement(By.cssSelector("button[type=submit]")).click();
// File upload: no dialog interaction needed
WebElement fileInput = driver.findElement(By.id("resume-upload"));
fileInput.sendKeys("/home/user/documents/resume.pdf");Calling sendKeys() on an input that isn't focused or visible (display:none, or covered by another element) will often throw ElementNotInteractableException. Ensure the element is displayed and enabled before typing, and scroll it into view if it's off-screen.
getAttribute("value") reads what's currently in the DOM's value property, which reflects real user typing and JavaScript updates alike. This differs from reading the HTML source's value="..." attribute, which only reflects the initial page load and won't update as the user types.
- sendKeys() appends to existing input content; call clear() first to avoid leftover text.
- sendKeys() fires real keyboard events, which matters for JS-driven live validation.
- form.submit() bypasses button-specific onclick handlers; clicking the actual button doesn't.
- Use getAttribute("value") to read input content, not getText(), which only returns visible text nodes.
- File uploads use sendKeys() with an absolute file path on the <input type=file> element — no OS dialog interaction is needed or possible.
- ElementNotInteractableException usually means the target isn't visible, focused, or enabled yet.
Practice what you learned
1. What happens if you call sendKeys("world") on a text field that already contains "hello" without calling clear() first?
2. Which method correctly reads back the current text typed into an <input> element?
3. How does clicking the actual submit button differ from calling WebElement.submit() on a form field?
4. How do you upload a file with Selenium to an <input type="file"> element?
5. What is the most likely cause of an ElementNotInteractableException when calling sendKeys()?
Was this page helpful?
You May Also Like
Finding Elements: findElement vs findElements
How Selenium locates elements in the DOM, the difference between findElement and findElements, and how to choose the right locator strategy.
Clicks and Keyboard Actions
How to perform clicks, hovers, drag-style holds, and keyboard shortcuts with Selenium's click() and the Actions class.
Handling Dropdowns and Checkboxes
How to work with native <select> dropdowns via the Select class, and correctly toggle checkboxes and radio buttons.