0
0
Selenium Pythontesting~15 mins

Handling pop-up windows in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify handling of pop-up window after clicking 'Open Popup' button
Preconditions (2)
Step 1: Locate and click the 'Open Popup' button on the main page
Step 2: Switch control to the newly opened pop-up window
Step 3: Verify the pop-up window title is 'Popup Window'
Step 4: Verify the pop-up window contains a paragraph with text 'Welcome to the popup!'
Step 5: Close the pop-up window
Step 6: Switch control back to the main window
Step 7: Verify the main window title is 'Main Page'
✅ Expected Result: The pop-up window opens and displays the correct title and text. After closing the pop-up, control returns to the main window with the correct title.
Automation Requirements - selenium
Assertions Needed:
Assert pop-up window title equals 'Popup Window'
Assert pop-up window contains paragraph text 'Welcome to the popup!'
Assert main window title equals 'Main Page' after closing pop-up
Best Practices:
Use explicit waits to wait for pop-up window to appear
Use driver.window_handles to switch between windows
Close pop-up window properly before switching back
Avoid hardcoded sleep statements
Use descriptive variable names for window handles
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

# Initialize the Chrome driver
with webdriver.Chrome() as driver:
    driver.get('https://example.com/mainpage')  # Replace with actual URL

    main_window = driver.current_window_handle

    # Click the button to open the popup
    open_popup_button = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.ID, 'open-popup'))
    )
    open_popup_button.click()

    # Wait for new window to appear
    WebDriverWait(driver, 10).until(
        lambda d: len(d.window_handles) > 1
    )

    # Switch to the popup window
    popup_window = [w for w in driver.window_handles if w != main_window][0]
    driver.switch_to.window(popup_window)

    # Assert popup window title
    WebDriverWait(driver, 10).until(EC.title_is('Popup Window'))
    assert driver.title == 'Popup Window', f"Expected popup title 'Popup Window' but got '{driver.title}'"

    # Assert popup contains expected paragraph text
    popup_paragraph = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.CSS_SELECTOR, 'p.popup-text'))
    )
    assert popup_paragraph.text == 'Welcome to the popup!', f"Expected popup text 'Welcome to the popup!' but got '{popup_paragraph.text}'"

    # Close the popup window
    driver.close()

    # Switch back to main window
    driver.switch_to.window(main_window)

    # Assert main window title
    WebDriverWait(driver, 10).until(EC.title_is('Main Page'))
    assert driver.title == 'Main Page', f"Expected main page title 'Main Page' but got '{driver.title}'"

This script starts by opening the main page URL and saving the main window handle.

It waits until the 'Open Popup' button is clickable, then clicks it to open the pop-up window.

It waits until a new window handle appears, then switches control to the new pop-up window.

It asserts the pop-up window's title and verifies the paragraph text inside it.

After verification, it closes the pop-up window and switches back to the main window.

Finally, it asserts the main window's title to confirm control returned correctly.

Explicit waits ensure the script waits for elements and windows properly without using fixed delays.

Using descriptive variable names and proper window handle management makes the code clear and maintainable.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Not switching back to the main window after closing the pop-up
Hardcoding window handles or assuming order
{'mistake': 'Using incorrect selectors for elements inside the pop-up', 'why_bad': "Selectors that don't match cause NoSuchElementException and test failures.", 'correct_approach': 'Use unique and stable locators like IDs or descriptive CSS selectors.'}
Bonus Challenge

Now add data-driven testing with 3 different pop-up button IDs and expected titles/texts

Show Hint