


How to Switch Between Multiple Browser Windows in Selenium with Python?
How to Handle Multiple Browser Windows in Selenium with Python
When working with Selenium automation, you may encounter situations where multiple browser windows appear. As the focus remains on the first opened window, navigating or performing actions in subsequent windows becomes challenging. To address this, a key method is the driver.switch_to.window().
Locating the Window Name
Contrary to the notion that driver.switch_to.window() requires a window name, it works on window handles instead. Determining the window handle can be achieved using the window_handles property.
How to Switch to New Window
To switch focus to the newly opened window, follow these steps:
- Before clicking the link that triggers the new window, record the current window handle using:
window_before = driver.window_handles[0]
- After clicking the link, retrieve the handle of the new window:
window_after = driver.window_handles[1]
- Utilize the switch_to.window(window_handle) method to direct the focus:
driver.switch_to.window(window_after)
Example
Consider the following code that navigates between multiple windows:
import unittest from selenium import webdriver class WindowHandling(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_window_switch(self): driver = self.driver driver.get("http://www.cdot.in") window_before = driver.window_handles[0] driver.find_element_by_xpath("//a[@href='http://www.cdot.in/home.htm']").click() window_after = driver.window_handles[1] driver.switch_to.window(window_after) driver.find_element_by_link_text("ATM").click() driver.switch_to.window(window_before) def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()
The above is the detailed content of How to Switch Between Multiple Browser Windows in Selenium with Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Fastapi ...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.
