drag_and_drop – Action Chains in Selenium Python

Selenium’s Python Module is built to perform automated testing with Python. ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keypress, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop. Action chain methods are used by advanced scripts where we need to drag an element, click an element, double click, etc.
This article revolves around drag_and_drop method on Action Chains in Python Selenium. drag_and_drop method holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
Syntax –

drag_and_drop(source, target)
Args –

  • source: The element to mouse down.
  • target: The element to mouse up.

Example –

filter_none

edit
close

play_arrow

link
brightness_4
code

<input type ="text" name ="passwd" id ="passwd-id" />

chevron_right


To find an element one needs to use one of the locating strategies, For example,

filter_none

edit
close

play_arrow

link
brightness_4
code

element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")

chevron_right


Now one can use drag_and_drop method as an Action chain as below –



drag_and_drop(source, target)

How to use drag_and_drop Action Chain method in Selenium Python ?

To demonstrate, drag_and_drop method of Action Chains in Selenium Python. Let’ s visit https://www.geeksforgeeks.org/ and operate on an element.

Program –

filter_none

edit
close

play_arrow

link
brightness_4
code

# import webdriver
from selenium import webdriver
  
# import Action chains 
from selenium.webdriver.common.action_chains import ActionChains
  
# create webdriver object
driver = webdriver.Firefox()
  
# get geeksforgeeks.org
  
# get source element 
source_element = driver.find_element_by_link_text("Courses")
  
# get target element 
target_element = driver.find_element_by_link_text("Hard")
  
# create action chain object
action = ActionChains(driver)
  
# drag and drop the item
action.drag_and_drop(source_element, target_element)
  
# perform the operation
action.perform()

chevron_right


Output –
action-chains-selenium-Python

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Software Developer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.