What are ActionChains class in Selenium with python?


We have the concept of ActionChains class in Selenium. These classes are generally used for automating interactions like context menu click, mouse button actions, key press and mouse movements.

These types of actions are mainly common in complex scenarios like drag and drop and hovering over an element on the page. The methods of the Action Chains class are utilized by advanced scripts. We can manipulate DOM with the help of Action Chains in Selenium.

The action chain object implements the ActionChains in the form of a queue and then executes the perform() method. On calling the method perform(), all the actions on action chains will be performed.

The method of creating an Action Chain object is listed below −

  • First we need to import the Action Chain class and then the driver will be passed as an argument to it.

  • Now all the operations of action chains can be done with the help of this object.

Syntax

Syntax for creating an object of Action Chains −

from selenium import webdriver

# import Action chains
from selenium.webdriver import ActionChains
# create webdriver object
driver = webdriver.Firefox()
# create action chain object
action = ActionChains(driver)

After creating an object of Action Chains, we can perform numerous operations one by one like a chain which is queued.

Syntax

m = driver.find_element_by_css_selector("#tutor")
submenu = driver.find_element_by_id("submenu")
actions = ActionChains(driver)
actions.move_to_element(m)
actions.click(submenu)
actions.perform()

Some of the important methods under the Action chains are listed below −

  • context_click() – This method performs right click operation on an element of the page.

  • click_and_hold() – This method performs the action of holding the left mouse button to an element of the page.

  • click() - This method performs click operation on an element of the page.

  • double_click() - This method performs double click operation on an element of the page.

  • drag_and_drop() - This method performs the action of holding the left mouse button on the source element. Then moves to the target element and finally releases the mouse button.

  • drag_and_drop() - This method performs the action of holding the left mouse button on the source element. Then moves to the target element and finally releases the mouse button.

  • drag_and_drop_by_offset() - This method performs the action of holding the left mouse button on the source element. Then moves to the target offset element and finally releases the mouse button.

  • move_to_element() - This method performs movement of mouse to the middle of the element on the page.

  • move_by_offset() - This method performs movement of mouse to an offset from the present position of the mouse.

  • key_up() – This method releases a modifier key.

  • key_down() – This method performs sending a key press only and not releasing it.

  • release() – This method performs releasing a held mouse button on an element.

  • pause() - This method performs stopping all inputs for a particular duration of time.

  • perform() - This method performs all the actions queued one after the other.

  • move_to_element_with_offset() - This method performs movement of the mouse by the offset of the element specified element on the page. The offsets are in respect to the top left corner of the element.

  • send_keys() – This method performs the action of sending keys to the present element which is focused.

  • reset_actions() – This method performs the action of resetting all the actions that are stored in local and on to the remote end.

Updated on: 29-Jul-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements