0
0
Selenium Pythontesting~15 mins

Click actions in Selenium Python - Build an Automation Script

Choose your learning style9 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