- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to perform drag and drop operation in Selenium with python?
We can perform drag and drop actions in Selenium with the help of Action Chains class. 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.
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.
Syntax
drag_and_drop(args1, args2)
Where, args1 is the element on which mouse down operation is done.
And args2 is the element on which mouse up operation is done.
#source source = driver.find_element_by_link_text("Tutorialspoint") #target target = driver.find_element_by_link_text("Selenium") #action chain object action = ActionChains(driver) # drag and drop operation action.drag_and_drop(source, target)
Example
Code Implementation for drag and drop operation.
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.keys import Keys #browser exposes an executable file #Through Selenium test we will invoke the executable file which will then #invoke actual browser driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://jqueryui.com/droppable/") #to refresh the browser driver.refresh() # identifying the source and target elements source= driver.find_element_by_id("draggable"); target= driver.find_element_by_id("droppable"); # action chain object creation action = ActionChains(driver) # drag and drop operation and the perform action.drag_and_drop(source, target).perform() driver.close()
- Related Articles
- How to perform drag and drop actions in WebdriverIO?
- How to do drag and drop action in Selenium?
- How to simulate HTML5 Drag and Drop in Selenium Webdriver?
- How to automate drag & drop functionality using Selenium WebDriver Java?
- How to use drag and drop in HTML5?
- How to use drag and drop in Android?
- How to work with JavaScript Drag and drop for touch devices?
- HTML5 drag and drop will not drop
- Full page drag and drop files website with HTML
- How to perform back and refresh in a browser in Selenium with python?
- How to stop dragend's default behavior in drag and drop?
- How to Add Drag and Drop in React using React Beautiful DnD
- How to perform mouse movement to an element in Selenium with python?
- How to perform bitwise AND operation on two images in OpenCV Python?
- How to select a drop-down menu option value with Selenium (Python)?
