0
0
Selenium Pythontesting~10 mins

Why complex interactions need Actions in Selenium Python - Test Execution Impact

Choose your learning style9 modes available
Test Overview

This test demonstrates why complex user interactions, like drag and drop, require Selenium's Actions class. It verifies that dragging an element moves it to the target location.

Test Code - Selenium
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import unittest

class TestDragAndDrop(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://jqueryui.com/droppable/')
        self.wait = WebDriverWait(self.driver, 10)

    def test_drag_and_drop(self):
        # Switch to demo frame
        self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '.demo-frame')))

        draggable = self.wait.until(EC.presence_of_element_located((By.ID, 'draggable')))
        droppable = self.wait.until(EC.presence_of_element_located((By.ID, 'droppable')))

        actions = ActionChains(self.driver)
        actions.drag_and_drop(draggable, droppable).perform()

        # Verify the drop was successful by checking text change
        drop_text = droppable.text
        self.assertEqual(drop_text, 'Dropped!')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 8 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window opens with empty tab-PASS
2Navigates to 'https://jqueryui.com/droppable/'Page loads showing draggable and droppable demo inside an iframePage loaded with demo frame presentPASS
3Waits for iframe and switches to itInside iframe containing draggable and droppable elementsIframe switched successfullyPASS
4Finds draggable element by ID 'draggable'Draggable element is visible and readyDraggable element locatedPASS
5Finds droppable element by ID 'droppable'Droppable element is visible and readyDroppable element locatedPASS
6Uses ActionChains to drag draggable element and drop it onto droppable elementDrag and drop action performed by Selenium-PASS
7Checks text of droppable element to verify drop successDroppable element text changed to 'Dropped!'Assert droppable.text == 'Dropped!'PASS
8Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: If ActionChains drag_and_drop is not used and simple click or send_keys are attempted, the draggable element does not move
Execution Trace Quiz - 3 Questions
Test your understanding
Why do we switch to the iframe before interacting with draggable and droppable elements?
ABecause the elements are inside the iframe and not accessible otherwise
BTo refresh the page content
CTo close the iframe
DTo maximize the browser window
Key Result
Use Selenium's Actions class for complex user interactions like drag and drop because simple commands cannot simulate these real user gestures accurately.