What are the Actions class in Selenium?

Selenium can perform mouse movements, keypress, hovering on an element, drag and drop actions, and so on with the help of the ActionsChains class. We have to create an instance of the ActionChains class which shall hold all actions in a queue.

Then the method - perform is invoked which actually performs the tasks in the order in which they are queued. We have to add the statement from selenium.webdriver import ActionChains to work with the ActionChains class.


#Method 1 - chained pattern

e =driver.find_element_by_css_selector(".txt")

a = ActionChains(driver)


#Method 2 - queued actions one after another

e =driver.find_element_by_css_selector(".txt")

a = ActionChains(driver)




In both the above methods, the actions are performed in sequence in which they are called, one by one.

Methods of ActionChains class are listed below −

  • click - it is used to click a web element.

  • click_and_hold - it is used to hold down the left mouse button on a web element.

  • double_click- it is used to double-click a web element.

  • context_click- it is used to right-click a web element.

  • drag_and_drop_by_offset - it is used to first perform pressing the left mouse on the source element, navigating to the target offset and finally releasing the mouse.

  • drag_and_drop - it is used to first perform pressing the left mouse on the source element, navigating to the target element and finally releasing the mouse.

  • key_up - it is used to release a modifier key.

  • key_down - it is used for a keypress without releasing it.

  • move_to_element - it is used to move the mouse to the middle of a webelement.

  • move_by_offset - it is used to move the mouse to an offset from the present mouse position.

  • perform - it is used to execute the queued actions.

  • move_to_element_by_offset - it is used to move the mouse by an offset of a particular webelement. The offsets are measured from the left-upper corner of the webelement.

  • release - it is used to release a held mouse button on a webelement.

  • pause - it is used to stop every input for a particular duration in seconds.

  • send_keys - it is used to send keys to the present active element.

  • reset_actions - it is used to delete all actions that are held locally and in remote.

Let us click on the link - Privacy Policy using the ActionChains methods −


from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')

#implicit wait time

#url launch

#identify element
s = driver.find_element_by_link_text("Privacy Policy")

#instance of ActionChains
a= ActionChains(driver)

#move to element


#get page title
print('Page title: ' + driver.title)

#driver quit