Simulate keystrokes
Selenium keys enum
See the official guide. You can also use these constants for sending keys to desktop applications.
Expand to see org.openqa.selenium.Keys enum constants
ADDALTARROW_DOWNARROW_LEFTARROW_RIGHTARROW_UPBACK_SPACECANCELCLEARCOMMANDCONTROLDECIMALDELETEDIVIDEDOWNENDENTEREQUALSESCAPEF1F10F12F2F3F4F5F6F7F8F9HELPHOMEINSERTLEFTLEFT_ALTLEFT_CONTROLLEFT_SHIFTMETAMULTIPLYNULLNUMPAD0NUMPAD1NUMPAD2NUMPAD3NUMPAD4NUMPAD5NUMPAD6NUMPAD7NUMPAD8NUMPAD9PAGE_DOWNPAGE_UPPAUSERETURNRIGHTSEMICOLONSEPARATORSHIFTSPACESUBTRACTTABUPZENKAKU_HANKAKU
Simulating multiple keys press
sendKeys(Keys.chord(Keys.LEFT_ALT, 'n'))
sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'c'))
sendKeys(Keys.chord(Keys.LEFT_CONTROL, 'v'))
Technical details
Use org.openqa.selenium.chord
Public static
java.lang.Stringchord (java.lang.CharSequence... value). Simulate pressing many keys at once in a chord. Takes a sequence ofKeys.XXXXor strings, appends each of the values to a string, adds the chord termination key (Keys.NULL), and returns the resultant string.When the low-level webdriver key handlers see
Keys.NULL, active modifier keys (CTRL, ALT, SHIFT, and so on) release via a keyup event. Value: characters to send. Returns a String representation of the char sequence.Public static
java.lang.Stringchord (java.lang.Iterable<java.lang.CharSequence\>value). Value: characters to send. Returns a String representation of the char sequence. Also, see chord(CharSequence...).
AutoIt keys for desktop automation
See the official guide.
!: This tells AutoIt to send an ALT keystroke. Therefore,sendKeys("This is text!a")sends the keys "This is text". Then, press ALT+a.
Some programs are sensitive to capital letters and ALT keys, for example, !A is different from !a. The first says ALT+SHIFT+A, the second is ALT+a. If in doubt, use lowercase.
+: This tells AutoIt to send a SHIFT keystroke. Therefore,sendKeys("Hell+o")sends the text "HellO".sendKeys("!+a")sends "ALT+SHIFT+a".^: This tells AutoIt to send a CONTROL keystroke. Therefore,sendKeys("^!a")sends "CTRL+ALT+a".
Some programs are sensitive about capital letters and CTRL keys, for example, ^A is different from ^a. The first says CTRL+SHIFT+A, the second is CTRL+a. If in doubt, use lowercase.
#: The hash sends a Windows keystroke. Therefore,sendKeys("#r")sends Win+r that launches theRun()dialog box.
If a user is holding down the Shift key when a SendKeys function begins, the text can be sent in uppercase. A workaround is to use sendKeys("{SHIFTDOWN}{SHIFTUP}") before other sendKeys operations.
Certain keyboards, for example, the Czech keyboard, send different characters when using the Shift Key or with CapsLock enabled while sending a character. Due to the sent AutoIt implementation, a CapsLocked character is sent as a Shifted character, so it will not work.
| Command (if zero flag) | Resulting keypress |
|---|---|
{!} | ! |
{#} | # |
{+} | + |
{^} | ^ |
{{} | { |
{}} | } |
{SPACE} | SPACE |
{ENTER} | ENTER key on the main keyboard |
{ALT} | ALT |
{BACKSPACE} or {BS} | BACKSPACE |
{DELETE} or {DEL} | DELETE |
{UP} | Up arrow |
{DOWN} | Down arrow |
{LEFT} | Left arrow |
{RIGHT} | Right arrow |
{HOME} | HOME |
{END} | END |
{ESCAPE} or {ESC} | ESCAPE |
{INSERT} or {INS} | INS |
{PGUP} | PageUp |
{PGDN} | PageDown |
{F1} - {F12} | Function keys |
{TAB} | TAB |
{PRINTSCREEN} | Print Screen key |
{LWIN} | Left Windows key |
{RWIN} | Right Windows key |
{NUMLOCK on} | NUMLOCK (on, off, toggle) |
{CAPSLOCK off} | CAPSLOCK (on, off, toggle) |
{SCROLLLOCK toggle} | SCROLLLOCK (on, off, toggle) |
{BREAK} | Used for Ctrl+Break processing. |
{PAUSE} | PAUSE |
{NUMPAD0} - {NUMPAD9} | Numpad digits |
{NUMPADMULT} | Numpad Multiply |
{NUMPADADD} | Numpad Add |
{NUMPADSUB} | Numpad Subtract |
{NUMPADDIV} | Numpad Divide |
{NUMPADDOT} | Numpad period |
{NUMPADENTER} | Enter key on the numpad |
{APPSKEY} | Windows App key |
{LALT} | Left ALT key |
{RALT} | Right ALT key |
{LCTRL} | Left CTRL key |
{RCTRL} | Right CTRL key |
{LSHIFT} | Left Shift key |
{RSHIFT} | Right Shift key |
{SLEEP} | Computer SLEEP key |
{ALTDOWN} | Holds the ALT key down until {ALTUP} is sent. |
{SHIFTDOWN} | Holds the SHIFT key down until {SHIFTUP} is sent. |
{CTRLDOWN} | Holds the CTRL key down until {CTRLUP} is sent. |
{LWINDOWN} | Holds the left Windows key down until {LWINUP} is sent. |
{RWINDOWN} | Holds the right Windows key down until {RWINUP} is sent. |
{ASC nnnn} | Sends the ALT+nnnn key combination. |
{UTF} | Types into a window (for example, RemoteApp, RDP, Citrix, VirtualBox) regardless of its active language sendKeys('{UTF 怒helloनमस्ते}'). |
{BROWSER_BACK} | Selects the browser Back button. |
{BROWSER_FORWARD} | Selects the browser Forward button. |
{BROWSER_REFRESH} | Selects the browser Refresh button. |
{BROWSER_STOP} | Selects the browser Stop button. |
{BROWSER_SEARCH} | Selects the browser Search button. |
{BROWSER_FAVORITES} | Selects the browser Favorites button. |
{BROWSER_HOME} | Launches the browser and go to the home page. |
{VOLUME_MUTE} | Mutes the volume. |
{VOLUME_DOWN} | Reduces the volume. |
{VOLUME_UP} | Increases the volume. |
{MEDIA_NEXT} | Selects the next track in a media player. |
{MEDIA_PREV} | Selects the previous track in a media player. |
{MEDIA_STOP} | Stops a media player. |
{MEDIA_PLAY_PAUSE} | Plays and pauses a media player. |
{LAUNCH_MAIL} | Launches an email application. |
{LAUNCH_MEDIA} | Launches a media player. |
{LAUNCH_APP1} | Launches user app1. |
{LAUNCH_APP2} | Launches user app2. |
{OEM_102} | Presses the angle bracket key or the backslash key on the RT 102-key keyboard. |
To send the ASCII value A (the same as pressing ALT+065 on the numeric keypad), do as follows:
sendKeys("{ASC 065}")
// When using 2 digit ASCII codes you must use a leading 0, otherwise an obsolete 437 code page is used.
To send UNICODE characters, enter a character code (decimal or hex), for example, this sends a Chinese character:
sendKeys("{ASC 2709}")
sendKeys("{ASC 0xA95}")
You can also send single keys repeatedly:
sendKeys("{DEL 4}") // Presses the DEL key 4 times
sendKeys("{S 30}") // Sends 30 'S' characters
sendKeys("+{TAB 4}") // Presses SHIFT+TAB 4 times
To hold a key down:
sendKeys("{a down}") // Holds the A key down
sendKeys("{a up}") // Releases the A key
To set the state of the capslock, numlock, and scrolllock keys:
sendKeys("{NumLock on}") // Turns the NumLock key on
sendKeys("{CapsLock off}") // Turns the CapsLock key off
sendKeys("{ScrollLock toggle}") // Toggles the state of ScrollLock
Most laptop computer keyboards have a special Fn key. This key cannot be simulated.
To disable the above special processing, set the flag parameter to 1. It is useful when you want to send some text copied from a variable and you want the text sent exactly as written.
For example, open Folder Options (in the control panel) and try the following:
| Command | Description |
|---|---|
sendKeys("{TAB}") | Navigate to next control (button, checkbox, and so on). |
sendKeys("+{TAB}") | Navigate to previous control. |
sendKeys("^{TAB}") | Navigate to next WindowTab (on a Tabbed dialog window). |
sendKeys("^+{TAB}") | Navigate to previous WindowTab. |
sendKeys("{SPACE}") | Can be used to toggle a checkbox or click a button. |
sendKeys("{+}") | Usually checks a checkbox (if it's a "real" checkbox.) |
sendKeys("{-}") | Usually unchecks a checkbox. |
sendKeys("{NumPadMult}") | Recursively expands folders in a SysTreeView32. |
Unicode mode for sendKeys()
A bot can type into a window (for example, RemoteApp, RDP, Citrix, VirtualBox) regardless of its active language using the new Unicode typing mode:
Example: Typing text in three languages
sendKeys('{UTF 怒helloनमस्ते}')
Examples
Browser keys
sendKeys(Keys.chord(Keys.LEFT_ALT, 'n'))
// By TAB > ARROW_DOWN > ARROW_DOWN > ENTER pull up 'Save As' and click context menu item
pressTab()
sendKeys(Keys.ARROW_DOWN)
sendKeys(Keys.ARROW_DOWN)
pressEnter()
Desktop keys
sendKeys('vim the-new-file.txt{ENTER}')
windowContent = copyPuttyWindowText()
sendKeys('aaaaaaa')
sendKeys('{TAB}{ENTER 2}')
sendKeys('{UP}{UP}{DOWN}{DOWN}')
sendKeys('i');
sendKeys('{UP}{DOWN}{RIGHT}{RIGHT}{TAB}{TAB}')
sendKeys('Hello, Robot{!}')
sendKeys('{CTRLDOWN}c{CTRLUP}')
sendKeys(':q{!}')
sendKeys('{ENTER}')
sendKeys('{CTRLDOWN}d{CTRLUP}')