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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open, ready to load URL | — | PASS |
| 2 | Navigates 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 |
| 3 | Waits until the button with id 'hold-button' is present | Button is found on the page | Element presence confirmed | PASS |
| 4 | Performs click and hold action on the button | Button is pressed and held down | — | PASS |
| 5 | Waits until the message element with id 'message' becomes visible | Message element is visible with text 'Button is being held' | Message text equals 'Button is being held' | PASS |
| 6 | Test ends and browser closes | Browser window closed | — | PASS |
click_and_hold() method do in Selenium WebDriver?click_and_hold() method simulates pressing and holding the mouse button down on a web element without releasing it.click_and_hold() with underscores, not camelCase.perform()perform() must be called at the end of the action chain.from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
browser.get('https://example.com')
element = browser.find_element('id', 'drag')
actions = ActionChains(browser)
actions.click_and_hold(element).perform()
print('Action performed')click_and_hold(element).perform(), which presses and holds the mouse button on the element.actions = ActionChains(driver)
element = driver.find_element('css selector', '#button')
actions.click_and_hold().perform()click_and_hold() requires the element to be passed as an argument to specify where to click and hold.click_and_hold(source), then move_to_element(target), then release(), and finally perform() to execute.Pass the test and this pattern moves unknown