Bird
Raised Fist0
Selenium Pythontesting~15 mins

Click actions in Selenium Python - Build an Automation Script

Choose your learning style10 modes available
Verify clicking the Submit button navigates to confirmation page
Preconditions (2)
Step 1: Locate the Submit button by its ID 'submit-btn'
Step 2: Click the Submit button
Step 3: Wait for the page to navigate to the confirmation page
✅ Expected Result: The browser navigates to 'https://example.com/confirmation' and the confirmation message is visible
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is 'https://example.com/confirmation'
Verify confirmation message element with ID 'confirmation-msg' is visible
Best Practices:
Use explicit waits to wait for elements and page navigation
Use By.ID locator for the Submit button and confirmation message
Handle exceptions if elements are not found or clickable
Automated Solution
Selenium Python
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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Sleeping pauses the test for a fixed time which may be too short or too long, causing flaky tests or slow execution.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait only as long as needed."}
Using incorrect locator types or brittle XPath expressions
Not handling exceptions when elements are not found or clickable
Bonus Challenge

Now add data-driven testing with 3 different form URLs and verify the click action on each.

Show Hint

Practice

(1/5)
1. What does the click() method do in Selenium Python?
easy
A. Finds a web element by locator
B. Types text into a web element
C. Waits for a web element to be visible
D. Simulates a mouse click on a web element

Solution

  1. Step 1: Understand the purpose of click()

    The click() method is used to simulate a mouse click on a web element, like a button or link.
  2. Step 2: Differentiate from other methods

    Typing text uses send_keys(), finding elements uses find_element(), and waiting uses explicit or implicit waits, not click().
  3. Final Answer:

    Simulates a mouse click on a web element -> Option D
  4. Quick Check:

    click() = mouse click [OK]
Hint: Click means pressing a button or link with the mouse [OK]
Common Mistakes:
  • Confusing click() with send_keys()
  • Thinking click() finds elements
  • Assuming click() waits for elements
2. Which of the following is the correct syntax to click a button with id 'submit' in Selenium Python?
easy
A. driver.find_element(By.ID, 'submit').click()
B. driver.click_element(By.ID, 'submit')
C. driver.find(By.ID, 'submit').click()
D. driver.click(By.ID, 'submit')

Solution

  1. Step 1: Identify correct method to find element

    The correct method is find_element(By.ID, 'submit') to locate the element by its id.
  2. Step 2: Use click() on the found element

    After finding the element, call click() to simulate the click.
  3. Final Answer:

    driver.find_element(By.ID, 'submit').click() -> Option A
  4. Quick Check:

    find_element + click() = correct syntax [OK]
Hint: Use find_element() then call click() on it [OK]
Common Mistakes:
  • Using non-existent methods like click_element()
  • Calling click() directly on driver
  • Using find() instead of find_element()
3. What will happen when the following code runs if the button with id 'login' is not visible on the page?
driver.find_element(By.ID, 'login').click()
medium
A. selenium.common.exceptions.ElementNotInteractableException is raised
B. The button will be clicked successfully
C. No action will happen, code continues silently
D. selenium.common.exceptions.NoSuchElementException is raised

Solution

  1. 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.
  2. Step 2: Identify exception raised when clicking hidden element

    When clicking a hidden element, Selenium raises ElementNotInteractableException, not NoSuchElementException which is for missing elements.
  3. Final Answer:

    selenium.common.exceptions.ElementNotInteractableException is raised -> Option A
  4. Quick Check:

    Click hidden element = ElementNotInteractableException [OK]
Hint: Clicking hidden elements raises ElementNotInteractableException [OK]
Common Mistakes:
  • Confusing NoSuchElementException with ElementNotInteractableException
  • Assuming click() silently fails
  • Expecting click() to work on hidden elements
4. You wrote this code but it throws an error:
button = driver.find_element(By.ID, 'submit')
button.click()

What is the most likely cause?
medium
A. You forgot to import By from selenium.webdriver.common.by
B. The button is disabled or not visible
C. The element with id 'submit' does not exist on the page
D. You used click() instead of send_keys()

Solution

  1. Step 1: Check element existence

    If the element did not exist, find_element would raise NoSuchElementException immediately, not at click().
  2. Step 2: Consider element state for click()

    If the element is found but disabled or hidden, click() raises ElementNotInteractableException, causing the error.
  3. Final Answer:

    The button is disabled or not visible -> Option B
  4. Quick Check:

    Click error often means element not interactable [OK]
Hint: Check if element is visible and enabled before clicking [OK]
Common Mistakes:
  • Assuming missing import causes click error
  • Confusing click() with send_keys() usage
  • Ignoring element visibility or enabled state
5. You want to click a button only after it becomes clickable. Which Selenium Python approach is best to ensure this?
hard
A. Use time.sleep(5) before clicking
B. Use driver.refresh() before clicking
C. Use WebDriverWait with element_to_be_clickable condition
D. Call click() immediately after find_element()

Solution

  1. Step 1: Understand waiting for element state

    Using WebDriverWait with element_to_be_clickable waits until the element is visible and enabled for clicking.
  2. 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.
  3. Final Answer:

    Use WebDriverWait with element_to_be_clickable condition -> Option C
  4. Quick Check:

    Wait for clickable element with WebDriverWait [OK]
Hint: Wait explicitly for clickable element before clicking [OK]
Common Mistakes:
  • Using fixed sleep instead of explicit wait
  • Clicking without waiting for element readiness
  • Refreshing page unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard