Keyboard actions
There are only 2 actions that can be accomplished with a keyboard: pressing down on a key, and releasing a pressed key. In addition to supporting ASCII characters, each keyboard key has a representation that can be pressed or released in designated sequences.
Keys
In addition to the keys represented by regular unicode, unicode values have been assigned to other keyboard keys for use with Selenium. Each language has its own way to reference these keys; the full list can be found here.
Use the Java Keys enum
Use the Python Keys class
Use the .NET static Keys class
Use the Ruby KEYS constant
Use the JavaScript KEYS constant
Use the Java Keys enum
Key down
new Actions(driver)
.keyDown(Keys.SHIFT)
.sendKeys("a")
.perform();
ActionChains(driver)\
.key_down(Keys.SHIFT)\
.send_keys("abc")\
.perform()
.KeyDown(Keys.Shift)
.SendKeys("a")
.Perform();
driver.action
.key_down(:shift)
.send_keys('a')
.perform
await driver.actions()
.keyDown(Key.SHIFT)
.sendKeys('a')
.perform()
Actions(driver)
.keyDown(Keys.SHIFT)
.sendKeys("a")
.perform()
Key up
new Actions(driver)
.keyDown(Keys.SHIFT)
.sendKeys("a")
.keyUp(Keys.SHIFT)
.sendKeys("b")
.perform();
ActionChains(driver)\
.key_down(Keys.SHIFT)\
.send_keys("a")\
.key_up(Keys.SHIFT)\
.send_keys("b")\
.perform()
new Actions(driver)
.KeyDown(Keys.Shift)
.SendKeys("a")
.KeyUp(Keys.Shift)
.SendKeys("b")
.Perform();
driver.action
.key_down(:shift)
.send_keys('a')
.key_up(:shift)
.send_keys('b')
.perform
await driver.actions()
.keyDown(Key.SHIFT)
.sendKeys('a')
.keyUp(Key.SHIFT)
.sendKeys('b')
.perform()
Actions(driver)
.keyDown(Keys.SHIFT)
.sendKeys("a")
.keyUp(Keys.SHIFT)
.sendKeys("b")
.perform()
Send keys
This is a convenience method in the Actions API that combines keyDown and keyUp commands in one action. Executing this command differs slightly from using the element method, but primarily this gets used when needing to type multiple characters in the middle of other actions.
Active Element
new Actions(driver)
.sendKeys("abc")
.perform();
ActionChains(driver)\
.send_keys("abc")\
.perform()
new Actions(driver)
.SendKeys("abc")
driver.action
.send_keys('abc')
.perform
await driver.actions()
.sendKeys('abc')
.perform()
Actions(driver)
.sendKeys("abc")
.perform()
Designated Element
new Actions(driver)
.sendKeys(textField, "Selenium!")
.perform();
text_input = driver.find_element(By.ID, "textInput")
ActionChains(driver)\
.send_keys_to_element(text_input, "abc")\
.perform()
driver.FindElement(By.TagName("body")).Click();
IWebElement textField = driver.FindElement(By.Id("textInput"));
new Actions(driver)
text_field = driver.find_element(id: 'textInput')
driver.action
.send_keys(text_field, 'Selenium!')
.perform
const textField = await driver.findElement(By.id('textInput'))
await driver.actions()
.sendKeys(textField, 'abc')
.perform()
val textField = driver.findElement(By.id("textInput"))
Actions(driver)
.sendKeys(textField, "Selenium!")
.perform()
Copy and Paste
Here’s an example of using all of the above methods to conduct a copy / paste action.
Note that the key to use for this operation will be different depending on if it is a Mac OS or not.
This code will end up with the text: SeleniumSelenium!
Keys cmdCtrl = Platform.getCurrent().is(Platform.MAC) ? Keys.COMMAND : Keys.CONTROL;
WebElement textField = driver.findElement(By.id("textInput"));
new Actions(driver)
.sendKeys(textField, "Selenium!")
.sendKeys(Keys.ARROW_LEFT)
.keyDown(Keys.SHIFT)
.sendKeys(Keys.ARROW_UP)
.keyUp(Keys.SHIFT)
.keyDown(cmdCtrl)
.sendKeys("xvv")
.keyUp(cmdCtrl)
.perform();
Assertions.assertEquals("SeleniumSelenium!", textField.getAttribute("value"));
cmd_ctrl = Keys.COMMAND if sys.platform == 'darwin' else Keys.CONTROL
ActionChains(driver)\
.send_keys("Selenium!")\
.send_keys(Keys.ARROW_LEFT)\
.key_down(Keys.SHIFT)\
.send_keys(Keys.ARROW_UP)\
.key_up(Keys.SHIFT)\
.key_down(cmd_ctrl)\
.send_keys("xvv")\
.key_up(cmd_ctrl)\
.perform()
var capabilities = ((WebDriver)driver).Capabilities;
String platformName = (string)capabilities.GetCapability("platformName");
String cmdCtrl = platformName.Contains("mac") ? Keys.Command : Keys.Control;
new Actions(driver)
.SendKeys("Selenium!")
.SendKeys(Keys.ArrowLeft)
.KeyDown(Keys.Shift)
.SendKeys(Keys.ArrowUp)
cmd_ctrl = driver.capabilities.platform_name.include?('mac') ? :command : :control
driver.action
.send_keys('Selenium!')
.send_keys(:arrow_left)
.key_down(:shift)
.send_keys(:arrow_up)
.key_up(:shift)
.key_down(cmd_ctrl)
.send_keys('xvv')
.key_up(cmd_ctrl)
.perform
const cmdCtrl = platform.includes('darwin') ? Key.COMMAND : Key.CONTROL
await driver.actions()
.click(textField)
.sendKeys('Selenium!')
.sendKeys(Key.ARROW_LEFT)
.keyDown(Key.SHIFT)
.sendKeys(Key.ARROW_UP)
.keyUp(Key.SHIFT)
.keyDown(cmdCtrl)
.sendKeys('xvv')
.keyUp(cmdCtrl)
.perform()
val cmdCtrl = if(platformName == Platform.MAC) Keys.COMMAND else Keys.CONTROL
val textField = driver.findElement(By.id("textInput"))
Actions(driver)
.sendKeys(textField, "Selenium!")
.sendKeys(Keys.ARROW_LEFT)
.keyDown(Keys.SHIFT)
.sendKeys(Keys.ARROW_UP)
.keyUp(Keys.SHIFT)
.keyDown(cmdCtrl)
.sendKeys("xvv")
.keyUp(cmdCtrl)
.perform()