Keyboard Actions is an exclusive feature of Selenium in
Python, that allows pressing keys through the keyboard such as ctrl+p,
shift+t, etc.,
When we want to use Mouse or Keyboard actions we should
create an ActionChains class object and import
"from selenium.webdriver import ActionChains", "from selenium.webdriver import Keys".
- Syntax:
- from selenium.webdriver import ActionChains, Keys
- #
- action_obj = ActionChains(driver)
Method for ActionChains Class:
perform(): perform-function in
the ActionChains is used to create a chain of actions or operations you want
to perform and execute the action.
Keyboard Keys used to send in ActionChians Class:
DECIMAL, LEFT_SHIFT, ALT, CONTROL, SHIFT, HELP, ENTER, ARROW_LEFT,
ARROW_RIGHT, ARROW_UP, ARROW_DOWN, BACK_SPACE, CANCEL, CLEAR, COMMAND,
DELETE, DIVIDE, END, EQUALS, ESCAPE, F1, F2, F3, F4, F5, F6, F7, F8, F9,
F10, F11, F12, HOME, INSERT, LEFT, LEFT_ALT, LEFT_CONTROL, META, MULTIPLY,
NULL, NUMPAD0, NUMPAD1, NUMPAD2, NUMPAD3, NUMPAD4, NUMPAD5, NUMPAD6,
NUMPAD7, NUMPAD8, NUMPAD9, PAGE_UP, PAGE_DOWN, PAUSE, RETURN, RIGHT,
SEMICOLON, SEPARATOR, SPACE, SUBTRACT, ADD, TAB, DOWN, UP.
send_keys(key_to_send): This
function sends keyboard input to a webpage.
- Syntax:
- a = ActionChains(driver)
- a.send_keys(Keys.PAGE_DOWN).perform()
key_down(value, element=None):
This function presses a Key without releasing it. It should only be used
with modifier keys such as CONTROL, ALT, and SHIFT.
- Syntax:
- a = ActionChains(driver)
- a.key_down(Keys.SHIFT).send_keys("testing").perform()
key_up(value, element=None):
This function releases a Key. It should only be used with modifier keys such
as CONTROL, ALT, and SHIFT.
- Syntax:
- a = ActionChains(driver)
- a.key_down(Keys.SHIFT).send_keys("testing").key_up(Keys.Shift).perform()
Keyboard actions can also be used in combination with Mouse
actions.
- Example: Mouse over to a element and then send the arrow_down key.
- a = ActionChains(driver)
- element = driver.find_element(By.ID, "abc")
- a.move_to_element(element).send_keys(Keys.ARROW_DOWN).perform()
No comments:
Post a Comment