How to Create and Use Action Class in Selenium Python
By Pooja Deshpande, Community Contributor - December 19, 2022
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.
Also Read: How to handle Action class in Selenium
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.
Also Read: Selenium with Python Tutorial
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 = webdriver.chrome()
- 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() driver.get("https://www.browserstack.com/") element = driver.find_element_by_link_text("Sign in") action = ActionChains(driver) action.click(on_element = element) action.perform()
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