Course Content
Testing Tools
Testing Tools
0/1
Sample Python Programs
Sample Python Programs
0/1
Selenium Python First Script
Selenium Python First Script
0/2
HTML Form elements
HTML Form elements
0/1
Handle HTML Frames
Handle HTML Frames
0/2
Handling Dropdown Lists
Handling Dropdown Lists
0/2
Selenium Action class
Selenium Action class
0/1
Python Database Programming
Python Database Programming
0/1
Selenium Python for Beginners [ 2024 ]
About Lesson

Selenium Action class

The Selenium Action class can perform various interactions on a web page, such as mouse movements, drag and drop, keyboard actions, etc.

from selenium.webdriver.common.action_chains import ActionChains

 

Example

In this example, we will test the following web page, which uses JQuery and displays two elements to support drag-and-drop operations. Before Test Before Drag and Drop After Test Drag and Drop After Test Here’s a basic example of how you can use the Action class in Python with Selenium:

# Google Homepage Test Example
# www.TestingDocs.com 

import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time

class GoogleTest(unittest.TestCase): 
    
    def setUp(self):
        self.driver = webdriver.Firefox()
        
        
    def test_method(self):
        driver=self.driver 
        driver.get("https://testingdocs2016.blogspot.com/2016/06/drag-and-drop.html")
        actions = ActionChains(driver)
        draggable = driver.find_element(By.ID,"dragme")
        droppable = driver.find_element(By.ID, "drophere")
        actions.drag_and_drop(draggable, droppable).perform()
        driver.implicitly_wait(60)
        self.assertEqual(droppable.text,"Successfully Dropped!") 
        
    def tearDown(self):
        self.driver.quit()
        
            
if __name__ == '__main__': 
    unittest.main()

Screenshot

Screenshot of the test in the Eclipse IDE

Selenium Drag and Drop Test