This method is used to act on the respective key by pressing, Without releasing it.
It should be only used with modifier keys (Control, Alt and Shift etc).
This method is used to release a modifier key(Control, Alt and Shift etc).
Example: pressing and releasing ctrl+c::
actions = ActionChains(driver) actions.key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
Example:
KeyBoard_operation.py
from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time
driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")
driver.get("http://www.dummypoint.com/Actions.html")
assert "Selenium Template" in driver.title
time.sleep(2)
wait = WebDriverWait(driver, 25, poll_frequency=1,
ignored_exceptions=[ElementNotVisibleException, NoSuchElementException])
ele = wait.until(ec.presence_of_element_located((By.ID,"user_input")))
ele.send_keys("Skill2Lead")
time.sleep(2)
# 1. Create the object for ActionChains class
actions = ActionChains(driver)
# 2. Click "Control+A" to select the entered data
actions.key_down(Keys.COMMAND).send_keys('a').key_up(Keys.COMMAND).perform()
time.sleep(5)
driver.quit()
Example:
KeyBoard_operation.py
from selenium import webdriver
from selenium.common.exceptions import ElementNotVisibleException, NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time
driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")
driver.get("http://www.dummypoint.com/Actions.html")
assert "Selenium Template" in driver.title
time.sleep(2)
wait = WebDriverWait(driver, 25, poll_frequency=1,
ignored_exceptions=[ElementNotVisibleException, NoSuchElementException])
ele = wait.until(ec.presence_of_element_located((By.ID,"user_input")))
ele.send_keys("Skill2Lead")
time.sleep(2)
# 1. Create the object for ActionChains class
actions = ActionChains(driver)
# 2. Click "Control+A" to select the entered data
actions.key_down(Keys.COMMAND).send_keys('a').key_up(Keys.COMMAND).perform()
time.sleep(2)
# 3. Click control+c to copy the entered data
actions.key_down(Keys.COMMAND).send_keys('c').key_up(Keys.COMMAND).perform()
time.sleep(2)
ele = wait.until(ec.presence_of_element_located((By.XPATH,"//input[@id='keyseditbox']")))
ele.click()
# 4. Past the entered data into another editbox
time.sleep(2)
actions.key_down(Keys.COMMAND).send_keys('v').key_up(Keys.COMMAND).perform()
time.sleep(2)
driver.quit()