Skip to content
CodeKruPro

Keyboard - Special Keys

56/70

Send Special Keys

The Keys class provides constants for special keys like ENTER, TAB, BACKSPACE, CONTROL, etc.

You can use them with sendKeys() to simulate keyboard shortcuts.

Task:

  1. Navigate to https://testkru.com/Elements/TextFields
  2. Type "Hello World" in the firstName field
  3. Select all text using Ctrl+A (Keys.chord(Keys.CONTROL, "a"))
  4. Delete the selected text using BACKSPACE
  5. Type "Selenium" in the now-empty field
  6. Print the final value

Expected output:

Final value: Selenium

About this lesson

Select all and delete text with Keys.chord in Selenium Java

Learn Selenium JavaAdvancedKeyboard Actions

How it works

The Keys enum holds the keys that have no character: Keys.ENTER, Keys.TAB, Keys.BACK_SPACE, arrows, function keys and modifiers such as Keys.CONTROL. You pass them to sendKeys() like text. There is no Keys.BACKSPACE; the constant is BACK_SPACE, and the other spelling does not compile.

Keys.chord(Keys.CONTROL, "a") builds one string that presses Control, types a, and ends with Keys.NULL, which releases the modifier. That matters when the chord is followed by more text in the same sendKeys() call; without it the rest would be typed with Control held. Sending the chord on its own selects all the text in the field.

Shortcuts depend on the operating system. The site's runner is Linux, where select-all is Control+A; Keys.COMMAND there does nothing. On a Mac you would use Keys.COMMAND, so cross-platform suites pick the modifier at run time.

About the assignment

The starter gives you the first-name field. You type some text, select it all with a key chord, delete it with the backspace key, type the replacement word from the task and print the field's value.

What you will do

  1. Type the first text from the task into the field.
  2. Send a chord of Control and a to select everything.
  3. Send Keys.BACK_SPACE to delete the selection.
  4. Type the replacement word, then read getAttribute("value") and print it after the label Final value:.

Common mistakes

  • Writing Keys.BACKSPACE

    The constant does not exist, so the code fails to compile with "cannot find symbol". Use Keys.BACK_SPACE.

  • Using the Mac modifier on the runner

    Keys.COMMAND does not select all on Linux, so the old text stays and the new word is added to it.

  • Skipping the select step

    Backspace alone removes one character, and the rest of the old text stays in the field.

In a real test suite

Key combinations are used to clear stubborn fields that ignore clear(), to test keyboard shortcuts in web apps, and to submit forms with Keys.ENTER. A helper that picks Control or Command from System.getProperty("os.name") keeps suites portable.

Lesson 55 held a mouse button. Lesson 57 holds a key with the Actions class.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/TextFields");
        
        WebElement field = driver.findElement(By.id("firstName"));
        
        // TODO: Type "Hello World"
        // TODO: Select all with Ctrl+A and delete with BACKSPACE
        // TODO: Type "Selenium"
        // TODO: Print the final value
        
        driver.quit();
    }
}