0
0
Selenium Pythontesting~15 mins

Simple alert acceptance in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Accept a simple alert popup
Preconditions (1)
Step 1: Locate the button that triggers the alert popup
Step 2: Click the button to open the alert
Step 3: Switch to the alert popup
Step 4: Accept the alert popup
✅ Expected Result: The alert popup is accepted and closed successfully
Automation Requirements - Selenium with Python
Assertions Needed:
Verify that the alert is present before accepting
Verify that after accepting the alert, no alert is present
Best Practices:
Use explicit waits to wait for the alert to be present
Use Selenium's Alert API to handle alert popups
Avoid hardcoded sleeps
Use clear and maintainable locators
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 WebDriver (e.g., Chrome)
driver = webdriver.Chrome()

try:
    driver.get('https://example.com/alert')  # Replace with actual URL

    # Locate the button that triggers the alert
    alert_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'alertButton'))
    )

    # Click the button to open the alert
    alert_button.click()

    # Wait for the alert to be present
    WebDriverWait(driver, 10).until(EC.alert_is_present())

    # Switch to the alert
    alert = driver.switch_to.alert

    # Assert alert text is not empty (optional check)
    assert alert.text != '', 'Alert text should not be empty'

    # Accept the alert
    alert.accept()

    # Verify alert is no longer present
    try:
        WebDriverWait(driver, 3).until(EC.alert_is_present())
        alert_present = True
    except TimeoutException:
        alert_present = False

    assert not alert_present, 'Alert should be closed after acceptance'

finally:
    driver.quit()

This script starts by opening the browser and navigating to the page with the alert button.

It waits explicitly for the alert button to be clickable, then clicks it to trigger the alert popup.

Next, it waits for the alert to appear using an explicit wait, then switches to the alert using Selenium's Alert API.

It asserts that the alert text is not empty to confirm the alert is present.

Then it accepts the alert to close it.

Finally, it verifies that the alert is no longer present by waiting briefly and catching a timeout exception if no alert appears.

The driver quits at the end to close the browser.

Common Mistakes - 3 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Hardcoded sleeps slow down tests and can cause flakiness if the alert appears faster or slower than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected_conditions to wait for the alert."}
Not switching to the alert before accepting
Using incorrect locator for the alert button
Bonus Challenge

Now add data-driven testing with 3 different alert buttons that trigger alerts with different messages

Show Hint