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
- Type the first text from the task into the field.
- Send a chord of Control and
ato select everything. - Send
Keys.BACK_SPACEto delete the selection. - Type the replacement word, then read
getAttribute("value")and print it after the labelFinal 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.COMMANDdoes 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.
Related lessons
- Clear and Replace Text
clear(), the usual way to empty a field. - Keyboard - Actions Class Key EventsHold a modifier across several keys with Actions.
- Type Text with sendKeys()
sendKeys()and how typing appends to a field.
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.
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();
}
}