What are the Actions class in Selenium?

SeleniumAutomation TestingTesting Tools

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.

Syntax

#Method 1 - chained pattern

e =driver.find_element_by_css_selector(".txt")

a = ActionChains(driver)

a.move_to_element(e).click().perform()

#Method 2 - queued actions one after another

e =driver.find_element_by_css_selector(".txt")

a = ActionChains(driver)

a.move_to_element(e)

a.click()

a.perform()

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 −

Example

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

#implicit wait time
driver.implicitly_wait(5)

#url launch
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")

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

#instance of ActionChains
a= ActionChains(driver)

#move to element
a.move_to_element(s)

#click
a.click().perform()

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

#driver quit
driver.close()

Output

raja
Updated on 08-Feb-2022 11:03:54

Advertisements