Skip to main content
SeleniumDecoded

Click and Type

Learn the fundamental Selenium interactions: clicking elements and typing text into input fields.

Selenium 3 & 4 Stable

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

Simple Click
Selenium 3 & 4 Stable
// Find and click a button
WebElement button = driver.findElement(By.id("submit"));
button.click();
// Or chain it
driver.findElement(By.id("submit")).click();
# Find and click a button
button = driver.find_element(By.ID, "submit")
button.click()
# Or chain it
driver.find_element(By.ID, "submit").click()
// Find and click a button
const button = await driver.findElement(By.id('submit'));
await button.click();
// Or chain it
await driver.findElement(By.id('submit')).click();
// Find and click a button
IWebElement button = driver.FindElement(By.Id("submit"));
button.Click();
// Or chain it
driver.FindElement(By.Id("submit")).Click();

Wait Before Clicking

Always wait for elements to be clickable:

Wait and Click
Selenium 3 & 4 Stable
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.):

JavaScript Click
Selenium 3 & 4 Medium
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

Type Text
Selenium 3 & 4 Stable
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:

Clear Then Type
Selenium 3 & 4 Stable
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

Using Special Keys
Selenium 3 & 4 Stable
import org.openqa.selenium.Keys;
WebElement input = driver.findElement(By.id("search"));
// Press Enter
input.sendKeys(Keys.ENTER);
// Press Tab
input.sendKeys(Keys.TAB);
// Keyboard shortcut (Ctrl+A)
input.sendKeys(Keys.chord(Keys.CONTROL, "a"));
// Clear using keyboard
input.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.DELETE);
from selenium.webdriver.common.keys import Keys
input_field = driver.find_element(By.ID, "search")
# Press Enter
input_field.send_keys(Keys.ENTER)
# Press Tab
input_field.send_keys(Keys.TAB)
# Keyboard shortcut (Ctrl+A)
input_field.send_keys(Keys.CONTROL, "a")
# Clear using keyboard
input_field.send_keys(Keys.CONTROL, "a", Keys.DELETE)
const { Key } = require('selenium-webdriver');
const input = await driver.findElement(By.id('search'));
// Press Enter
await input.sendKeys(Key.ENTER);
// Press Tab
await input.sendKeys(Key.TAB);
// Keyboard shortcut (Ctrl+A)
await input.sendKeys(Key.chord(Key.CONTROL, 'a'));
// Clear using keyboard
await input.sendKeys(Key.chord(Key.CONTROL, 'a'), Key.DELETE);
using OpenQA.Selenium;
IWebElement input = driver.FindElement(By.Id("search"));
// Press Enter
input.SendKeys(Keys.Enter);
// Press Tab
input.SendKeys(Keys.Tab);
// Keyboard shortcut (Ctrl+A)
input.SendKeys(Keys.Control + "a");
// Clear using keyboard
input.SendKeys(Keys.Control + "a" + Keys.Delete);

Common Special Keys

KeyJava/C#PythonJavaScript
EnterKeys.ENTERKeys.ENTERKey.ENTER
TabKeys.TABKeys.TABKey.TAB
EscapeKeys.ESCAPEKeys.ESCAPEKey.ESCAPE
BackspaceKeys.BACK_SPACEKeys.BACKSPACEKey.BACK_SPACE
DeleteKeys.DELETEKeys.DELETEKey.DELETE
Arrow UpKeys.ARROW_UPKeys.ARROW_UPKey.ARROW_UP
ControlKeys.CONTROLKeys.CONTROLKey.CONTROL
ShiftKeys.SHIFTKeys.SHIFTKey.SHIFT
AltKeys.ALTKeys.ALTKey.ALT

Login Form Example

Complete Login Form
Selenium 3 & 4 Stable
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:

  1. Wait for element to be clickable
  2. Scroll element into view
  3. Wait for overlays to disappear
  4. Use JavaScript click as last resort

Stale Element Reference

Cause: DOM changed after finding the element.

Solution:

  1. Re-locate the element before interacting
  2. Use explicit waits with fresh locators
  3. Avoid storing element references for long periods

Best Practices

  1. Always wait before interacting: Use elementToBeClickable or visibilityOf
  2. Clear inputs before typing: Prevents appending to existing text
  3. Verify state changes: Wait for URL change, element appearance, etc.
  4. Handle different input types: Some inputs need special handling (file uploads, rich text editors)

Next Steps