0
0
Selenium Pythontesting~10 mins

Click and hold in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a webpage, finds a button, clicks and holds it, then verifies that the hold action triggered a visible change on the page.

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 TestClickAndHold(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/click-and-hold')
        self.wait = WebDriverWait(self.driver, 10)

    def test_click_and_hold_button(self):
        driver = self.driver
        wait = self.wait
        button = wait.until(EC.presence_of_element_located((By.ID, 'hold-button')))
        actions = ActionChains(driver)
        actions.click_and_hold(button).perform()
        # Verify that holding the button changes text
        message = wait.until(EC.visibility_of_element_located((By.ID, 'message')))
        self.assertEqual(message.text, 'Button is being held')

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

if __name__ == '__main__':
    unittest.main()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to load URL-PASS
2Navigates to 'https://example.com/click-and-hold'Page loads with a button having id 'hold-button' and a hidden message element with id 'message'-PASS
3Waits until the button with id 'hold-button' is presentButton is found on the pageElement presence confirmedPASS
4Performs click and hold action on the buttonButton is pressed and held down-PASS
5Waits until the message element with id 'message' becomes visibleMessage element is visible with text 'Button is being held'Message text equals 'Button is being held'PASS
6Test ends and browser closesBrowser window closed-PASS
Failure Scenario
Failing Condition: The button with id 'hold-button' is not found or the message text does not update after click and hold
Execution Trace Quiz - 3 Questions
Test your understanding
What Selenium method is used to perform the click and hold action?
Aactions.click_and_hold(element).perform()
Belement.click()
Cactions.double_click(element).perform()
Ddriver.find_element().click()
Key Result
Use Selenium's ActionChains for complex mouse actions like click and hold, and always verify the expected UI change after the action to confirm success.