About this lesson
keyDown and keyUp in Selenium Java: hold a key with the Actions class
Learn Selenium JavaAdvancedKeyboard Actions
How it works
keyDown(Keys.SHIFT) presses a key and keeps it down; keyUp(Keys.SHIFT) releases it. Everything typed in between is typed with the key held, so holding Shift while sending lower-case letters gives upper-case text, exactly as on a real keyboard.
Actions send keys to whatever has focus, not to an element you name. Without focus, the letters go to the page body and the field stays empty. click(field) at the start of the chain, keyDown(field, Keys.SHIFT) or sendKeys(field, text) all focus the field first.
Releasing matters. If the chain ends without keyUp(), Shift stays down: in this runner later Actions typing comes out in capitals, and so does the next WebElement.sendKeys().
About the assignment
The starter gives you the first-name field and an Actions object. You type a word with Shift held so it comes out in capitals, then read the field's value.
What you will do
- Start the chain by focusing the field, for example with
click()on it. - Add
keyDown(Keys.SHIFT), thensendKeys()with the lower-case word from the task, thenkeyUp(Keys.SHIFT). - Finish the chain with
perform(). - Read
getAttribute("value")and print it after the labelTyped:.
Common mistakes
Not focusing the field
The letters go to the page body, and the field's value is an empty string.
Leaving out keyUp()
Shift stays held, so the next text you type, through Actions or the next
sendKeys()call, comes out in capitals.Typing the word in capitals
The point is that the held key changes the text. Sending upper-case letters skips the lesson, though the output would look the same.
In a real test suite
Held keys power Shift-click range selection in lists, Ctrl-click multi-selection, and keyboard shortcuts in editors and spreadsheets. Chains such as keyDown(Keys.CONTROL).click(first).click(second).keyUp(Keys.CONTROL) select several items in one gesture.
Lesson 56 sent a key chord in one sendKeys(). Lesson 58 starts on JavascriptExecutor, which runs JavaScript in the page.
Related lessons
- Keyboard - Special KeysKey chords sent through
sendKeys(). - Handle Multi-Select DropdownMulti-select lists, where the
Selectclass does what a Ctrl-click withkeyDown()would do by hand. - Click and Hold ActionHold a mouse button the same way.
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;
import org.openqa.selenium.interactions.Actions;
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"));
Actions actions = new Actions(driver);
// TODO: Click the field, hold Shift, type "hello", release Shift, then perform the chain
// TODO: Print the field's value after the label "Typed: "
driver.quit();
}
}