App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide How to Create and Use Action Class in Selenium Python

How to Create and Use Action Class in Selenium Python

By Pooja Deshpande, Community Contributor -

Table of Contents

    Selenium comes with a class called ActionChains to perform various complex actions like context menu click, drag and drop, mouse hover etc, which are not possible by the WebDriver actions in Python. Action chain object is implemented in the form of a queue and the actions in the queue are executed using the perform() method.

    The ActionChains class has multiple methods that help to perform these actions. Few of these methods have been listed below in the article.

    • drag_and_drop(): drag_and_drop method is used to drag and drop by holding the left mouse button on the source element, moving to the target element and then releasing on the target element. It takes two parameters as input, source and target element.



    • context_click(): context_click method is used to right click on the required element.It has one argument, element on which the action has to be performed. If you don’t pass any argument then it right clicks on the current position.



    • double_click():  As the name says, this method double clicks on an element or the current position. It takes one argument that is the element on which double click has to be performed. If nothing is passed then it clicks on the current mouse position.



    • send_keys(): send_keys method is used to send keys to an element in focus. Key constants can be found in Keys class.



    • click(): Click method is used to perform a click on the current element. You need to pass the element as an argument on which click has to be performed.



    • move_to_element(): move_to_element method performs mouse movement to the middle of the element.



    • move_to_element_with_offset(): move_to_element_with_offset method is used to move mouse to element by an offset. It has three parameters – element, xoffset and yoffset. 



    • perform(): perform method performs all the actions stored inside an actions object.



    These are just a few methods listed however there are more methods in ActionChains class that help to perform various actions on webpage elements.

    Now, let us see an example on how we can create an object of an action class and perform actions using that object.

    • Import action chains
    from selenium.webdriver.common.action_chains import ActionChains
    • Create a webdriver object
    driver =   
    • Create an action chains object
    action = ActionChains(driver)

    After creating this object we can perform various actions on web page elements using this object.

    Below is an example where we are clicking on the “Sign in” link using the action object.

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    driver = webdriver.Firefox()
    element = driver.find_element_by_link_text("Sign in")
    action = ActionChains(driver) = element)

    ActionChains class comes with various methods that help to perform complex actions on webelements. You can go for ActionChains class when it becomes difficult to perform actions by a WebDriver. 

    All Selenium tests must be run on real devices for accurate results. BrowserStack supports over 3000+ real browsers and devices. You can run parallel tests using a Cloud Selenium Grid and get accurate and faster results. Detect bugs before users do by testing software in real user conditions with BrowserStack Automate.

    Features of BrowserStack Automate:

    • Run hundreds of concurrent tests
    • Integrate with popular languages like Python, Java and top CI/CD tools like Jenkins, CircleCI
    • Instant access to 2000+ Real Devices and Browsers
    • Comprehensive Debugging using video recordings, automated screenshots of errors
    • Enterprise-Grade Security & GDPR Compliance

    Try BrowserStack Automate Now

    Automation Frameworks Automation Testing

    Featured Articles

    Get Current URL in Selenium using Python: Tutorial

    Start Selenium Testing with Python: Automated Testing of a User Signup Form

    App & Browser Testing Made Easy

    Seamlessly test across 20,000+ real devices with BrowserStack