Click and Type
Learn the fundamental Selenium interactions: clicking elements and typing text into input fields.
The most common interactions in web automation are clicking elements and typing text. This guide covers the basics and common pitfalls.
Clicking Elements
Basic Click
// Find and click a buttonWebElement button = driver.findElement(By.id("submit"));button.click();
// Or chain itdriver.findElement(By.id("submit")).click();# Find and click a buttonbutton = driver.find_element(By.ID, "submit")button.click()
# Or chain itdriver.find_element(By.ID, "submit").click()// Find and click a buttonconst button = await driver.findElement(By.id('submit'));await button.click();
// Or chain itawait driver.findElement(By.id('submit')).click();// Find and click a buttonIWebElement button = driver.FindElement(By.Id("submit"));button.Click();
// Or chain itdriver.FindElement(By.Id("submit")).Click();Wait Before Clicking
Always wait for elements to be clickable:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until( ExpectedConditions.elementToBeClickable(By.id("submit")));button.click();wait = WebDriverWait(driver, 10)
button = wait.until( EC.element_to_be_clickable((By.ID, "submit")))button.click()const button = await driver.wait( until.elementLocated(By.id('submit')), 10000);await driver.wait(until.elementIsEnabled(button), 10000);await button.click();WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement button = wait.Until( ExpectedConditions.ElementToBeClickable(By.Id("submit")));button.Click();JavaScript Click (Fallback)
When regular click fails (element obscured, etc.):
WebElement button = driver.findElement(By.id("submit"));JavascriptExecutor js = (JavascriptExecutor) driver;js.executeScript("arguments[0].click();", button);button = driver.find_element(By.ID, "submit")driver.execute_script("arguments[0].click();", button)const button = await driver.findElement(By.id('submit'));await driver.executeScript('arguments[0].click();', button);IWebElement button = driver.FindElement(By.Id("submit"));IJavaScriptExecutor js = (IJavaScriptExecutor)driver;js.ExecuteScript("arguments[0].click();", button);Warning: JavaScript click bypasses visibility checks. Use only when the regular click fails and you’ve verified the element should be clickable.
Typing Text
Basic Input
WebElement input = driver.findElement(By.id("username"));input.sendKeys("myusername");input_field = driver.find_element(By.ID, "username")input_field.send_keys("myusername")const input = await driver.findElement(By.id('username'));await input.sendKeys('myusername');IWebElement input = driver.FindElement(By.Id("username"));input.SendKeys("myusername");Clear and Type
Always clear existing text before typing:
WebElement input = driver.findElement(By.id("search"));input.clear();input.sendKeys("new search term");input_field = driver.find_element(By.ID, "search")input_field.clear()input_field.send_keys("new search term")const input = await driver.findElement(By.id('search'));await input.clear();await input.sendKeys('new search term');IWebElement input = driver.FindElement(By.Id("search"));input.Clear();input.SendKeys("new search term");Special Keys
import org.openqa.selenium.Keys;
WebElement input = driver.findElement(By.id("search"));
// Press Enterinput.sendKeys(Keys.ENTER);
// Press Tabinput.sendKeys(Keys.TAB);
// Keyboard shortcut (Ctrl+A)input.sendKeys(Keys.chord(Keys.CONTROL, "a"));
// Clear using keyboardinput.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);from selenium.webdriver.common.keys import Keys
input_field = driver.find_element(By.ID, "search")
# Press Enterinput_field.send_keys(Keys.ENTER)
# Press Tabinput_field.send_keys(Keys.TAB)
# Keyboard shortcut (Ctrl+A)input_field.send_keys(Keys.CONTROL, "a")
# Clear using keyboardinput_field.send_keys(Keys.CONTROL, "a", Keys.DELETE)const { Key } = require('selenium-webdriver');
const input = await driver.findElement(By.id('search'));
// Press Enterawait input.sendKeys(Key.ENTER);
// Press Tabawait input.sendKeys(Key.TAB);
// Keyboard shortcut (Ctrl+A)await input.sendKeys(Key.chord(Key.CONTROL, 'a'));
// Clear using keyboardawait input.sendKeys(Key.chord(Key.CONTROL, 'a'), Key.DELETE);using OpenQA.Selenium;
IWebElement input = driver.FindElement(By.Id("search"));
// Press Enterinput.SendKeys(Keys.Enter);
// Press Tabinput.SendKeys(Keys.Tab);
// Keyboard shortcut (Ctrl+A)input.SendKeys(Keys.Control + "a");
// Clear using keyboardinput.SendKeys(Keys.Control + "a" + Keys.Delete);Common Special Keys
| Key | Java/C# | Python | JavaScript |
|---|---|---|---|
| Enter | Keys.ENTER | Keys.ENTER | Key.ENTER |
| Tab | Keys.TAB | Keys.TAB | Key.TAB |
| Escape | Keys.ESCAPE | Keys.ESCAPE | Key.ESCAPE |
| Backspace | Keys.BACK_SPACE | Keys.BACKSPACE | Key.BACK_SPACE |
| Delete | Keys.DELETE | Keys.DELETE | Key.DELETE |
| Arrow Up | Keys.ARROW_UP | Keys.ARROW_UP | Key.ARROW_UP |
| Control | Keys.CONTROL | Keys.CONTROL | Key.CONTROL |
| Shift | Keys.SHIFT | Keys.SHIFT | Key.SHIFT |
| Alt | Keys.ALT | Keys.ALT | Key.ALT |
Login Form Example
public void login(String username, String password) { WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
// Wait for and fill username WebElement usernameField = wait.until( ExpectedConditions.visibilityOfElementLocated(By.id("username")) ); usernameField.clear(); usernameField.sendKeys(username);
// Fill password WebElement passwordField = driver.findElement(By.id("password")); passwordField.clear(); passwordField.sendKeys(password);
// Click login button WebElement loginButton = wait.until( ExpectedConditions.elementToBeClickable(By.id("login")) ); loginButton.click();
// Wait for login to complete wait.until(ExpectedConditions.urlContains("/dashboard"));}def login(driver, username, password): wait = WebDriverWait(driver, 10)
# Wait for and fill username username_field = wait.until( EC.visibility_of_element_located((By.ID, "username")) ) username_field.clear() username_field.send_keys(username)
# Fill password password_field = driver.find_element(By.ID, "password") password_field.clear() password_field.send_keys(password)
# Click login button login_button = wait.until( EC.element_to_be_clickable((By.ID, "login")) ) login_button.click()
# Wait for login to complete wait.until(EC.url_contains("/dashboard"))async function login(driver, username, password) { // Wait for and fill username const usernameField = await driver.wait( until.elementLocated(By.id('username')), 10000 ); await usernameField.clear(); await usernameField.sendKeys(username);
// Fill password const passwordField = await driver.findElement(By.id('password')); await passwordField.clear(); await passwordField.sendKeys(password);
// Click login button const loginButton = await driver.wait( until.elementLocated(By.id('login')), 10000 ); await driver.wait(until.elementIsEnabled(loginButton), 10000); await loginButton.click();
// Wait for login to complete await driver.wait(until.urlContains('/dashboard'), 10000);}public void Login(string username, string password){ WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
// Wait for and fill username IWebElement usernameField = wait.Until( ExpectedConditions.ElementIsVisible(By.Id("username")) ); usernameField.Clear(); usernameField.SendKeys(username);
// Fill password IWebElement passwordField = driver.FindElement(By.Id("password")); passwordField.Clear(); passwordField.SendKeys(password);
// Click login button IWebElement loginButton = wait.Until( ExpectedConditions.ElementToBeClickable(By.Id("login")) ); loginButton.Click();
// Wait for login to complete wait.Until(ExpectedConditions.UrlContains("/dashboard"));}Common Issues
Element Not Interactable
Cause: Element is hidden, disabled, or covered by another element.
Solution:
- Wait for element to be clickable
- Scroll element into view
- Wait for overlays to disappear
- Use JavaScript click as last resort
Stale Element Reference
Cause: DOM changed after finding the element.
Solution:
- Re-locate the element before interacting
- Use explicit waits with fresh locators
- Avoid storing element references for long periods
Best Practices
- Always wait before interacting: Use
elementToBeClickableorvisibilityOf - Clear inputs before typing: Prevents appending to existing text
- Verify state changes: Wait for URL change, element appearance, etc.
- Handle different input types: Some inputs need special handling (file uploads, rich text editors)
Next Steps
- Forms and Inputs - Complete form handling
- Dropdowns and Selects - Select elements and checkboxes
- Keyboard Actions - Advanced keyboard interactions
- Explicit Waits - Wait for elements before interacting
Copy-paste recipes for this topic
- Clear and Type Into a React or Angular Input Reliably
Replace the value of a framework-controlled input when clear() does not stick, using select-all plus type and the native setter fallback.
- Click With a JavaScript Fallback
Try a real click first, then fall back to a JavaScript click only for the specific interception and visibility errors, with a log line so fallbacks are visible.