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 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