Test Overview
This test opens a web page, finds a button by its ID, clicks it, and verifies that a confirmation message appears.
This test opens a web page, finds a button by its ID, clicks it, and verifies that a confirmation message appears.
from selenium import webdriver 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 ClickActionTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/click-test') def test_click_button_shows_message(self): driver = self.driver # Wait until the button is clickable button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'click-me')) ) button.click() # Wait for the confirmation message to appear message = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'confirmation')) ) self.assertEqual(message.text, 'Button clicked!') 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 the page | — | PASS |
| 2 | Navigates to 'https://example.com/click-test' | Page loads with a button having ID 'click-me' | — | PASS |
| 3 | Waits until the button with ID 'click-me' is clickable | Button is visible and interactable | Button element is found and clickable | PASS |
| 4 | Clicks the button | Button is clicked, triggering page action | — | PASS |
| 5 | Waits until the confirmation message with ID 'confirmation' is visible | Confirmation message appears on the page | Confirmation message element is visible | PASS |
| 6 | Checks that the confirmation message text equals 'Button clicked!' | Message text is 'Button clicked!' | message.text == 'Button clicked!' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | — | PASS |
click() method do in Selenium Python?click()click() method is used to simulate a mouse click on a web element, like a button or link.send_keys(), finding elements uses find_element(), and waiting uses explicit or implicit waits, not click().click() = mouse click [OK]find_element(By.ID, 'submit') to locate the element by its id.click() on the found elementclick() to simulate the click.driver.find_element(By.ID, 'login').click()
ElementNotInteractableException, not NoSuchElementException which is for missing elements.button = driver.find_element(By.ID, 'submit') button.click()
find_element would raise NoSuchElementException immediately, not at click().click() raises ElementNotInteractableException, causing the error.WebDriverWait with element_to_be_clickable waits until the element is visible and enabled for clicking.time.sleep() is unreliable and slows tests; immediate click may fail if element not ready; refreshing page is unrelated.Pass the test and this pattern moves unknown