Selenium · Browser automation foundations

How do you simulate keyboard and mouse input in Selenium?

MediumkeyboardmouseinputUpdated Oct 2026
Quiz this track

Short answer

Use sendKeys with Keys constants for typing and chords, and the Actions API for keyDown, keyUp, hover and pointer movement. Clear fields explicitly when replacing text rather than relying on select-all.

sendKeys types into a focused element and supports special keys. Keys.chord combines modifiers and a key in one call. For held keys across multiple actions, or for pointer movement without clicking, use Actions.

java
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("selenium" + Keys.ENTER);

search.sendKeys(Keys.chord(Keys.CONTROL, "a"));
search.sendKeys("webdriver");
search.sendKeys(Keys.chord(Keys.CONTROL, "a"), Keys.BACK_SPACE);

new Actions(driver)
    .keyDown(Keys.SHIFT)
    .sendKeys("hello")
    .keyUp(Keys.SHIFT)
    .perform();
Chords handle one-step shortcuts; keyDown and keyUp hold a modifier across steps.
  • Keys.ENTER, TAB, ESCAPE and the arrow keys are common special inputs
  • Keys.chord presses modifiers together for one shortcut
  • keyDown and keyUp are for holding a modifier across multiple actions
  • The modifier key differs by platform; use Keys.CONTROL or Keys.COMMAND deliberately
  • clear() empties an input but may not fire the same events as a user deletion
  • Typing into an element requires it to be focusable and interactable

For a field with a mask or a rich editor, clear plus sendKeys may not reproduce the application's event handling. Check the actual behaviour and, if needed, select all and delete through the UI. Avoid OS-level keyboard tools because they are not portable and break in headless or remote runs.

Keyboard shortcuts are often platform-specific. A test that hard-codes CONTROL fails on macOS unless the test environment is explicitly standardised.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between Keys.chord and keyDown?
  2. 2Why does CONTROL fail on macOS?
  3. 3How do you reliably replace existing text in an input?
  4. 4Why avoid Robot for keyboard input?

Say it in the interview

I use sendKeys with Keys constants for typing and one-step shortcuts, and Actions keyDown and keyUp when a modifier must stay held across steps. For replacing text I clear the field or select all and delete, then verify the value. I avoid OS-level automation because it is not portable and fails in headless or Grid runs. I also make modifier keys explicit so the suite does not silently depend on one operating system.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Selenium mock interview and get scored feedback on depth, clarity and confidence.