How do you switch to a new window in Selenium for Python?
Switching to a New Window in Selenium for Python
In Selenium automation using Python, handling multiple browser windows is a common task. When a link is clicked that opens a new window, the focus remains on the original window, preventing actions from being performed in the new one.
To switch the focus from the background window to the newly opened one, you can employ the driver.switch_to.window() method. However, this requires knowing the window's name.
Finding the Window's Name
To retrieve the window's name, you can utilize the window_handles property. It returns a list of currently active window handles. The window handle for the original window can be stored before clicking the link:
window_before = driver.window_handles[0]
After clicking the link, the window handle for the new window can be obtained:
window_after = driver.window_handles[1]
Switching to the New Window
Once you have the window handles, you can switch to the new window using the switch_to.window() method:
driver.switch_to.window(window_after)
Now, the focus is on the new window, allowing you to perform actions such as clicking elements and navigating the page.
Code Example
The following code demonstrates how to switch to a new window in Selenium for Python:
import unittest from selenium import webdriver class GoogleOrgSearch(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_google_search_page(self): driver = self.driver driver.get("http://www.cdot.in") window_before = driver.window_handles[0] print(window_before) 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) print(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 do you switch to a new window in Selenium for 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

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...

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...

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...

Using python in Linux terminal...

Fastapi ...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
