How To Handle Mouse and Keyboard Interactions in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “How To Handle Child Window, Frames, Alerts in Selenium Python“. In this tutorial, we will learn How To Handle Mouse & Keyboard Interactions in Selenium Python.Â
The mouse and keyboard movements can be simulated with the help of Selenium WebDriver. The actions like a double click, right-click, mouse movement, key press, mouse button connection, hovering and drag and drop and so on are performed with the help of ActionChains class in Selenium.
While we are using methods of ActionChains object, all the actions are kept in a queue. After executing the perform () method, all the actions are triggered one by one in a sequence. The actions can be queued up one after the other or placed in a chained order.
Syntax with queueing the actions.
acts = ActionChains (driver) acts.move_to_element (m) acts.click (h) acts.perform ()
Syntax with chaining the actions.
acts = ActionChains(driver). move_to_element (m). click (h).perform ()
Let us see some of the mouse actions methods under ActionChains:
click (args) – This method shall click on a web element. The argument args is the web element to be clicked. If the parameter to the method is ignored, then the current mouse position is clicked.
click_and_hold (args) – This method shall click on the current mouse location without releasing it. The argument args is the web element to mouse down. If the parameter to the method is ignored, then the current mouse position is clicked.
double_click (args) – This method shall double click on a web element. The argument args is the web element to be double-clicked. If the parameter to the method is ignored, then the current mouse position is double-clicked.
context_click (args) – This method shall right click on a web element. The argument args is the web element to be right-clicked. If the parameter to the method is ignored, then the current mouse position is right-clicked.
drag_and_drop (s, d) – This method is used to hold down the mouse from the element at the source, then move to the destination element and finally release the mouse. The argument s refers to the location of the source element to mouse down and the argument d refers to the location of the destination to mouse up.
drag_and_drop_by_offset (s, offsetX, offsetY) – This method is used to hold down the mouse from the element at the source, then move to the destination element offset, and finally release the mouse. The argument s refers to the location of the source element to mouse down. The argument offsetX refers to the X offset to move to. The argument offsetY refers to the Y offset to move to.
move_to_element (args) – This method is used to move to the middle of the web element. The args is the web element to be moved to.
release (args) – This method is used to release a held mouse button on a web element. The argument args is the web element to mouse up. If the parameter to the method is ignored, then the current mouse position is released.
Let us see some of the keyboard methods under ActionChains:
send_keys (keys) – This method is used to send keys to the present active element. The argument keys are keys to send. The modifier keys are available from the Keys class.
key_down (v, args) – This method is used to do a key press without releasing it. This method can be used only with the Control, Alt, and Shift modifier keys. The argument v is a modifier key available from the Keys class. The argument args is the web element to send keys. If that parameter to the method is ignored, it sends keys to the present active element.
key_up (v, args) – This method is used to release a modifier key. The argument v is a modifier key available from the Keys class. The argument args is the web element to send keys. If that parameter to the method is ignored, it sends keys to the present active element.
Code Implementation for mouse hover action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("https://www.softwaretestingmaterial.com/") # to identify the element l = driver.find_element_by_link_name ("Blog") # Action Chains object creation act = ActionChains(driver) # move to the middle of element act.move_to_element (l) # perform the action act.perform () # to close the browser driver.quit ()
Code Implementation for drag and drop action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("http://jqueryui.com/droppable/") driver.switch_to.frame (0) # to identify the source element s = driver.find_element_by_css_selector ("#draggable") # to identify the target element t = driver.find_element_by_css_selector ("#droppable") # Action Chains object creation act = ActionChains(driver) # to perform drag and drop of element act.drag_and_drop (s, t) # perform the action act.perform () # to close the browser driver.quit ()
Code Implementation for right-clicking action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("http://swisnl.github.io/jQuery-contextMenu/demo.html") # to identify the element s = driver.find_element_by_css_selector (".context-menu-one") # Action Chains object creation act = ActionChains(driver) # to perform right click on element act.context_click (s) # perform the action act.perform () # to close the browser driver.quit ()
Code Implementation for double-clicking action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("http://api.jquery.com/dblclick/") driver.switch_to.frame (0) # to identify the element s = driver.find_element_by_css_selector ("html>body>div") # Action Chains object creation act = ActionChains(driver) # to perform double click on element act.double_click (s) # perform the action act.perform () # to close the browser driver.quit ()
Code Implementation for clicking action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("https://www.softwaretestingmaterial.com") # to identify the element s = driver.find_element_by_link_text ("Blog") # Action Chains object creation act = ActionChains(driver) # to perform click on element act.click (s) # perform the action act.perform () # to close the browser driver.quit ()
Code Implementation for key up and down action.
# import the webdriver from selenium import webdriver # import the ActionChains from selenium.webdriver import ActionChains # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("https://www.softwaretestingmaterial.com") # to identify the element s = driver.find_element_by_css_selector ("#form-field-name") # Action Chains object creation act = ActionChains(driver) # to perform click on element act.click (s) # to press SHIFT key act.key_down (Keys.SHIFT) # to send keys on the element act.send_keys ("Python") # to release SHIFT key act.key_up (Keys.SHIFT) # perform the queued actions act.perform () # to close the browser driver.quit ()
In the next article, we will learn Assertions in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial