Switching Between Windows in Selenium Python
When automating web applications with Selenium in Python, one common challenge is handling multiple browser windows or tabs. This is especially relevant when clicking on links that open in a new window—such as external sites, pop-ups, or file downloads. By default, Selenium interacts with only the main browser window. To interact with others, you must explicitly switch between them.
In this blog, we’ll dive into how to switch between multiple windows or tabs using Selenium in Python, complete with code examples and best practices.
Why Window Switching is Important
Real-world websites often open new tabs or windows for:
- Payment gateways
- Terms and conditions
- Login through third-party providers (Google, Facebook)
- Download confirmations or previews
To perform actions on these new windows (e.g., clicking buttons, verifying content), you must switch Selenium’s control from the main window to the newly opened one.
Getting Started with Selenium in Python
First, install Selenium if you haven’t already:
bash
Copy
Edit
pip install selenium
Ensure you have the correct WebDriver for your browser (e.g., ChromeDriver for Chrome).
python
Copy
Edit
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
Working with Window Handles
Each browser window or tab opened by Selenium is assigned a unique identifier called a window handle.
python
Copy
Edit
main_window = driver.current_window_handle
all_windows = driver.window_handles
driver.current_window_handle returns the current active window.
driver.window_handles returns a list of all window handles opened by Selenium.
Example: Switching Between Windows
Here’s a complete example demonstrating how to switch between windows:
python
Copy
Edit
from selenium import webdriver
import time
# Launch browser
driver = webdriver.Chrome()
driver.get("https://demoqa.com/browser-windows")
# Click the button that opens a new tab
driver.find_element("id", "tabButton").click()
# Wait to ensure the new tab is open
time.sleep(2)
# Get handles
main_window = driver.current_window_handle
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
# Perform actions in the new window
print("Title of new window:", driver.title)
print("Text in new window:", driver.find_element("id", "sampleHeading").text)
# Close new window and switch back
driver.close()
driver.switch_to.window(main_window)
# Continue with main window
print("Back to main window:", driver.title)
driver.quit()
Best Practices
- Always store the main window handle before opening a new one.
- Use waits (time.sleep or WebDriverWait) to ensure the new window is fully loaded.
- Close the child window after use to avoid resource leaks.
- Use try-finally or teardown methods to ensure windows are cleaned up in tests.
Common Use Cases
- Verifying links that open in new tabs
- Handling OAuth logins via pop-up windows
- Automating PDF or document previews
- Testing ads or embedded services that open externally
Conclusion
Mastering window switching in Selenium Python is crucial for creating reliable and comprehensive test automation scripts. Whether you're testing third-party integrations, pop-ups, or multi-tab workflows, Selenium’s window-handling capabilities let you interact with any browser window just like a real user would.
With these techniques, you can automate even the most complex browser interactions with ease. Keep practicing, and soon you'll be able to handle any multi-window scenario with confidence.
Learn Selenium with Python Training Course
Read More: Automating Checkbox and Radio Buttons with Selenium Python
Visit Quality Thought Training Institute in Hyderabad
Get Direction
Comments
Post a Comment