How to handle multiple browser windows in Selenium?

  IHUB Talent: The Best Selenium with Python Training in Hyderabad with Live Internship

IHUB Talent offers the best Selenium with Python training in Hyderabad, designed to equip students with practical skills and industry-relevant knowledge. Our comprehensive program covers everything from the basics of Selenium and Python to advanced automation techniques, ensuring that students gain a solid understanding of test automation in real-world scenarios.

With a strong focus on hands-on learning, IHUB Talent provides students with opportunities to work on live projects and gain experience in automating tests for web applications. The training includes step-by-step guidance on setting up Selenium WebDriver, integrating it with Python, and using frameworks like Pay test and Unit test for efficient test management.

What sets IHUB Talent apart is our live internship program. Students not only learn from industry experts but also get the chance to apply their knowledge on live projects, making them job-ready from day one. Our trainers, who are professionals with years of experience, provide personalized mentorship to ensure that every student gets the attention they need to succeed.

In Selenium, XPath is one of the most powerful locators to find elements on a webpage. XPath (XML Path Language) lets you navigate through elements and attributes in an XML/HTML document.

Great question ๐Ÿ‘
In Selenium, web applications often open new browser windows or tabs (for example, after clicking a link or a button). To handle multiple windows, you use window handles.


๐Ÿ”‘ Key Concepts

  • driver.window_handles → Returns a list of all open window handles (unique IDs for each window/tab).

  • driver.current_window_handle → Returns the handle of the currently active window.

  • driver.switch_to.window(handle) → Switches WebDriver’s focus to a specific window.


๐Ÿ“ Step-by-Step Approach

  1. Get the main window handle.

  2. Perform the action that opens a new window/tab.

  3. Get all window handles.

  4. Switch control to the new window using switch_to.window().

  5. Perform actions in the new window.

  6. Switch back to the main window if needed.


Example in Python

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("https://example.com")

# Step 1: Store the main window handle
main_window = driver.current_window_handle

# Step 2: Click something that opens a new window
driver.find_element("xpath", "//a[@id='openNewWindow']").click()

# Step 3: Get all window handles
all_windows = driver.window_handles

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

# Step 5: Perform actions in the new window
print("New window title:", driver.title)
driver.find_element("xpath", "//button[text()='Confirm']").click()
time.sleep(2)

# Step 6: Switch back to main window
driver.switch_to.window(main_window)
print("Back to main window:", driver.title)

driver.quit()

Pro Tips

  • Always store the main window handle before switching.

  • Use driver.close() to close only the current window, or driver.quit() to close all.

  • Some apps open tabs instead of windows—handling is the same.

  • You can also loop through driver.window_handles if multiple child windows open.


Would you like me to also show you how to handle pop-ups (alerts) along with multiple windows since they’re often confused in interviews?

Read More

How to locate elements using XPath in Selenium?

Visit I HUB TALENT Training Institute In Hyderabad

Comments

Popular posts from this blog

What is Selenium and how is it used with Python?

What is Selenium in Python automation testing?

How do you install Selenium in Python?