Click actions in Selenium Python - Build an Automation Script
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 from selenium.common.exceptions import TimeoutException # Initialize the Chrome WebDriver with webdriver.Chrome() as driver: driver.get('https://example.com/form') try: # Wait until the Submit button is clickable submit_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, 'submit-btn')) ) # Click the Submit button submit_button.click() # Wait until URL changes to confirmation page WebDriverWait(driver, 10).until( EC.url_to_be('https://example.com/confirmation') ) # Wait until confirmation message is visible confirmation_msg = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'confirmation-msg')) ) # Assertions assert driver.current_url == 'https://example.com/confirmation', f"Expected URL to be 'https://example.com/confirmation' but got {driver.current_url}" assert confirmation_msg.is_displayed(), "Confirmation message is not visible" print('Test Passed: Click action navigated to confirmation page successfully.') except TimeoutException as e: print(f'Test Failed: Timeout waiting for element or page - {e}') except AssertionError as e: print(f'Test Failed: Assertion error - {e}')
This script uses Selenium WebDriver with Python to automate the click action test.
First, it opens the form page URL.
It waits explicitly for the Submit button to be clickable to avoid clicking too early.
Then it clicks the Submit button.
After clicking, it waits for the URL to change to the confirmation page URL.
It also waits for the confirmation message element to be visible on the page.
Assertions check that the URL is correct and the confirmation message is visible.
Exceptions are caught and printed to help identify failures.
This approach ensures the test is stable and clear.
Now add data-driven testing with 3 different form URLs and verify the click action on each.
Practice
click() method do in Selenium Python?Solution
Step 1: Understand the purpose of
Theclick()click()method is used to simulate a mouse click on a web element, like a button or link.Step 2: Differentiate from other methods
Typing text usessend_keys(), finding elements usesfind_element(), and waiting uses explicit or implicit waits, notclick().Final Answer:
Simulates a mouse click on a web element -> Option DQuick Check:
click()= mouse click [OK]
- Confusing click() with send_keys()
- Thinking click() finds elements
- Assuming click() waits for elements
Solution
Step 1: Identify correct method to find element
The correct method isfind_element(By.ID, 'submit')to locate the element by its id.Step 2: Use
After finding the element, callclick()on the found elementclick()to simulate the click.Final Answer:
driver.find_element(By.ID, 'submit').click() -> Option AQuick Check:
find_element + click() = correct syntax [OK]
- Using non-existent methods like click_element()
- Calling click() directly on driver
- Using find() instead of find_element()
driver.find_element(By.ID, 'login').click()
Solution
Step 1: Understand element visibility requirement
Selenium requires the element to be visible and enabled to perform click(). If the element is hidden, click() fails.Step 2: Identify exception raised when clicking hidden element
When clicking a hidden element, Selenium raisesElementNotInteractableException, notNoSuchElementExceptionwhich is for missing elements.Final Answer:
selenium.common.exceptions.ElementNotInteractableException is raised -> Option AQuick Check:
Click hidden element = ElementNotInteractableException [OK]
- Confusing NoSuchElementException with ElementNotInteractableException
- Assuming click() silently fails
- Expecting click() to work on hidden elements
button = driver.find_element(By.ID, 'submit') button.click()
What is the most likely cause?
Solution
Step 1: Check element existence
If the element did not exist,find_elementwould raiseNoSuchElementExceptionimmediately, not at click().Step 2: Consider element state for click()
If the element is found but disabled or hidden,click()raisesElementNotInteractableException, causing the error.Final Answer:
The button is disabled or not visible -> Option BQuick Check:
Click error often means element not interactable [OK]
- Assuming missing import causes click error
- Confusing click() with send_keys() usage
- Ignoring element visibility or enabled state
Solution
Step 1: Understand waiting for element state
UsingWebDriverWaitwithelement_to_be_clickablewaits until the element is visible and enabled for clicking.Step 2: Compare with other options
time.sleep()is unreliable and slows tests; immediate click may fail if element not ready; refreshing page is unrelated.Final Answer:
Use WebDriverWait with element_to_be_clickable condition -> Option CQuick Check:
Wait for clickable element with WebDriverWait [OK]
- Using fixed sleep instead of explicit wait
- Clicking without waiting for element readiness
- Refreshing page unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
