You are currently viewing How To Handle Multiple Windows in Selenium Python
How To Handle Multiple Windows in Selenium Python

How To Handle Multiple Windows in Selenium Python

How To Handle Multiple Windows in Selenium Python

In web automation testing, Handling multiple windows in Selenium Python is a crucial part of web automation testing. Whether you’re clicking a link that opens in a new tab or testing a web application that consists of multiple windows, it’s important to manage these windows efficiently using Selenium.

In this blog post, we’ll walk you through a Python code example to handle multiple windows in Selenium using Chrome WebDriver. This approach ensures that your automated tests run smoothly and effectively, increasing the reliability and robustness of your test scripts.

Setting Up the Environment

First, make sure you have Python and Selenium installed in your computer. You can install Selenium using pip:

pip install selenium

You’ll also need the Chrome WebDriver, which you can download from the official site

Step-by-Step Breakdown of Handling Multiple Windows in Selenium

Step 1: Import Required Libraries

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 time import sleep

Import Statements: We import the necessary modules and functions from Selenium and the Python standard library.

Step 2: Initialize Chrome WebDriver and Maximize Window

# Initialize the Chrome WebDriver
driver = webdriver.Chrome()
driver.maximize_window()
  • Initialization: We initialize the Chrome WebDriver using webdriver.Chrome().
  • Maximize Window: We maximize the browser window for better visibility and interaction.

Step 3: Navigate to the Website

# Navigate to the website
driver.get("https://mrwhotheengineer.com")

Navigation: We navigate to the desired website by providing its URL to driver.get().

Step 4: Set Up WebDriverWait

# Set up WebDriverWait
wait = WebDriverWait(driver, 10)

WebDriverWait Initialization: We set up WebDriverWait with a timeout of 10 seconds, which means WebDriver will wait up to 10 seconds for the conditions defined in the subsequent steps to be met.

Step 5: Get the Handle of the Main Window

# Get the handle of the main window
main_window = driver.current_window_handle

Main Window Handle: We get the handle of the main window using driver.current_window_handle. This handle is essential for switching back to the main window after interacting with the new windows.

Step 6: Click a Button to Open a New Window

# Click a button that opens a new window
automation_practice_button = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='Automation Practice']")))
automation_practice_button.click()

Element Click and WebDriverWait: We used WebDriverWait to wait for an element with the specified XPath to become clickable. Once the element is clickable, we click on it to open a new window.

Step 7: Wait for the New Window to Open

# Wait for the new window to open
wait.until(EC.number_of_windows_to_be(2))

Wait for New Window: We use wait.until(EC.number_of_windows_to_be(2)) to wait for two windows to be open (the main window and the new window).

Step 8: Get All Window Handles and Switch to the New Window

# Get all window handles
all_windows = driver.window_handles

# Switch to the new window
for window in all_windows:
    if window != main_window:
        driver.switch_to.window(window)
        break
  • Get All Window Handles: We get all window handles using driver.window_handles.
  • Switch to New Window: We loop through all window handles and switch to the window that is not the main window.

Step 9: Interact with Elements on the New Window

# Now you can interact with elements on the new window
second_page_button = wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[text()='Button'])[1]")))
second_page_button.click()
  • Element Interaction: We use WebDriverWait to wait for a button element with the specified XPath to become clickable. Once the button is clickable, we click on it.

Step 10: Close the New Window and Switch Back to the Main Window

# Close the new window
driver.close()

# Switch back to the main window
driver.switch_to.window(main_window)
  • Close New Window: We close the new window using driver.close().
  • Switch Back to Main Window: We switch back to the main window using driver.switch_to.window(main_window).

Step 11: Continue with Operations on the Main Window and Close It

# Continue with operations on the main window if needed
sleep(3)

# Close the main window
driver.close()

Continue and Close Main Window: We add a delay using sleep(3) to wait for any further operations on the main window and then close the main window using driver.close().

This step-by-step breakdown explains the entire process of handling multiple windows in Selenium using Python. Each step is crucial for efficiently managing and interacting with multiple browser windows during automated testing.

Complete Code

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 time import sleep

# Initialize the Chrome WebDriver
driver = webdriver.Chrome()
driver.maximize_window()

# Navigate to the website
driver.get("https://mrwhotheengineer.com")

# Set up WebDriverWait
wait = WebDriverWait(driver, 10)

# Get the handle of the main window
main_window = driver.current_window_handle

# Click a button that opens a new window
automation_practice_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Automation Practice']")))
automation_practice_button.click()

# Wait for the new window to open
wait.until(EC.number_of_windows_to_be(2))

# Get all window handles
all_windows = driver.window_handles

# Switch to the new window
for window in all_windows:
    if window != main_window:
        driver.switch_to.window(window)
        break

# Interact with elements on the new window
second_page_button = wait.until(EC.element_to_be_clickable((By.XPATH, "(//button[text()='Button'])[1]")))
second_page_button.click()

# Wait for the action to complete
sleep(3)

# Close the new window
driver.close()

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

# Continue with operations on the main window if needed

# Wait for the action to complete
sleep(3)

# Close the main window
driver.close()

You can also watch video for better understanding

Leave a Reply