Selenium Webdriver - Action Class

Selenium can perform mouse movements, key press, 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.
The syntax for ActionChains class is as follows −
#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.
The methods of ActionChains class are listed below −
click − It is used to click a webelement.
click − It is used to click a webelement.
click_and_hold − It is used to hold down the left mouse button on a webelement.
click_and_hold − It is used to hold down the left mouse button on a webelement.
double_click − It is used to double click a webelement.
double_click − It is used to double click a webelement.
context_click − It is used to right click a webelement.
context_click − It is used to right click a webelement.
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_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.
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_up − It is used to release a modifier key.
key_down − It is used for a keypress without releasing it.
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_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.
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.
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.
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.
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.
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.
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.
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 −
The code implementation for ActionChains class is as follows −
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()
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the page title of the application(obtained from the driver.title method) - About Privacy Policy at Tutorials Point - Tutorialspoint gets printed in the console.

    46 Lectures 
     5.5 hours 

 Aditya Dua

    296 Lectures 
     146 hours 

 Arun Motoori

    411 Lectures 
     38.5 hours 

 In28Minutes Official

    22 Lectures 
     7 hours 

 Arun Motoori

    118 Lectures 
     17 hours 

 Arun Motoori

    278 Lectures 
     38.5 hours 

 Lets Kode It
 Print
 Add Notes
 Bookmark this page