0
0
Selenium Pythontesting~10 mins

Handling pop-up windows in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, clicks a button that opens a pop-up window, switches to the pop-up, verifies its title, closes it, and returns to the main window.

Test Code - Selenium
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
import unittest

class TestPopupHandling(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/popup')

    def test_popup_window(self):
        driver = self.driver
        main_window = driver.current_window_handle

        # Click button that opens popup
        button = WebDriverWait(driver, 10).until(
            EC.element_to_be_clickable((By.ID, 'open-popup-btn'))
        )
        button.click()

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

        # Switch to popup window
        for handle in driver.window_handles:
            if handle != main_window:
                driver.switch_to.window(handle)
                break

        # Verify popup title
        popup_title = driver.title
        self.assertEqual(popup_title, 'Popup Window')

        # Close popup
        driver.close()

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

        # Verify main window title
        main_title = driver.title
        self.assertEqual(main_title, 'Main Page')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and opens browserBrowser opens and navigates to https://example.com/popup-PASS
2Finds button with ID 'open-popup-btn' and clicks itMain page displayed with button visible-PASS
3Waits until a new window appearsTwo windows open: main and popupCheck that number of window handles > 1PASS
4Switches to the popup windowPopup window is active-PASS
5Checks popup window title equals 'Popup Window'Popup window title is visibleAssert popup_title == 'Popup Window'PASS
6Closes popup windowPopup window closed, main window still open-PASS
7Switches back to main windowMain window active-PASS
8Checks main window title equals 'Main Page'Main window title visibleAssert main_title == 'Main Page'PASS
9Test ends and browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Popup window does not open or has wrong title
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test do after clicking the popup button?
ACloses the main window immediately
BRefreshes the page
CWaits for a new window to open
DSwitches to an iframe
Key Result
Always store the main window handle before opening a popup, so you can switch back to it after closing the popup window.