0
0
Selenium Pythontesting~15 mins

Why alert handling prevents test failures in Selenium Python - Automation Benefits in Action

Choose your learning style9 modes available
Handle unexpected alert on form submission
Preconditions (2)
Step 1: Enter 'testuser' in the username input field
Step 2: Click the submit button
Step 3: If an alert appears, accept the alert
Step 4: Verify the page URL changes to 'https://example.com/form-success'
✅ Expected Result: The alert is accepted without causing test failure and the page navigates to the success URL
Automation Requirements - Selenium with Python
Assertions Needed:
Verify alert is present and accepted
Verify current URL is 'https://example.com/form-success'
Best Practices:
Use explicit waits to detect alert presence
Use try-except block to handle NoAlertPresentException
Avoid hardcoded sleeps
Use By.ID locator strategy for elements
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 NoAlertPresentException, TimeoutException

# Setup WebDriver (assumes chromedriver is in PATH)
driver = webdriver.Chrome()

try:
    driver.get('https://example.com/form')

    # Enter username
    username_input = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, 'username'))
    )
    username_input.send_keys('testuser')

    # Click submit button
    submit_button = driver.find_element(By.ID, 'submit-btn')
    submit_button.click()

    # Wait for alert and accept it if present
    try:
        WebDriverWait(driver, 5).until(EC.alert_is_present())
        alert = driver.switch_to.alert
        alert.accept()
    except TimeoutException:
        # No alert appeared, continue
        pass

    # Verify URL changed to success page
    WebDriverWait(driver, 10).until(
        EC.url_to_be('https://example.com/form-success')
    )
    assert driver.current_url == 'https://example.com/form-success', f"Expected URL to be 'https://example.com/form-success' but got {driver.current_url}"

finally:
    driver.quit()

This script opens the form page and waits for the username input to appear before typing 'testuser'. It then clicks the submit button. After clicking, it waits up to 5 seconds for an alert to appear. If the alert appears, it accepts it to prevent the test from failing due to unhandled alert. If no alert appears, it continues without error. Finally, it waits for the URL to change to the success page and asserts this change. The use of explicit waits ensures the script waits only as long as needed, avoiding fixed delays. The try-except block for alert handling prevents test failure if no alert is present.

Common Mistakes - 3 Pitfalls
Not handling alerts causing test to fail
Using time.sleep instead of explicit waits
Hardcoding XPath locators for common elements
Bonus Challenge

Now add data-driven testing with 3 different usernames to verify alert handling for each input

Show Hint