Open In App

How to switch to new window in Selenium for Python?

Selenium is the most used Python tool for testing and automation for the web. But the problem occurs when it comes to a scenario where users need to switch between windows in chrome. Hence, selenium got that cover as well. Selenium uses these methods for this task-

Steps to cover:






# import modules
from selenium import webdriver  
import time  
  
# provide the path for chromedriver
PATH = "C:/chromedriver.exe"  
  
# pass on the path to driver for working
driver = webdriver.Chrome(PATH)  




# Open login yahoo for sample url
driver.get("https://login.yahoo.com/")  
  
# print title of yahoo window
print("First window title = " + driver.title)
  
# Clicks on privacy and it opens in new window
driver.find_element_by_class_name("privacy").click()
  
# switch window in 7 seconds
time.sleep(7)  
  
# window_handles[1] is a second window
driver.switch_to.window(driver.window_handles[1])
  
# prints the title of the second window
print("Second window title = " + driver.title)
  
# window_handles[0] is a first window
driver.switch_to.window(driver.window_handles[0])
  
# prints windows id
print(driver.window_handles)  

Output:

First window title = Yahoo
Second window title = Welcome to the Verizon Media Privacy Policy | Verizon Media Policies
['CDwindow-F25D48D2602CBD780FB2BE8B34A3BEAC', 'CDwindow-A80A74DFF7CCD47F628AF860F3D46913']



Explanation:

The program will first open yahoo then open privacy on yahoo in a new tab then again switch back to the yahoo tab in 7 seconds i.e first window.

Optional Case: If the user needs to open a new window and switch between them.

Example 1:




# Open the new window
driver.execute_script("window.open()")
  
# window switch to new 3rd window
driver.switch_to.window(driver.window_handles[2])
  
# get the new url in window 3
  
# print the 3rd window title
print(driver.title)

Output:

GeeksforGeeks | A computer science portal for geeks

Explanation:

The program enables to open a new window with get() method having parameter as geeks for geeks URL and prints the title of that window.

Example 2:




# import modules
from selenium import webdriver
import time
  
# assign path and driver
PATH = "C:/chromedriver.exe"
driver = webdriver.Chrome(PATH)
  
# assign URL
driver.get("https://login.yahoo.com/")
print("First window title = " + driver.title)
  
# switch to new window
driver.find_element_by_class_name("privacy").click()
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[1])
print("Second window title = " + driver.title)
  
# switch to new window
driver.execute_script("window.open()")
print(driver.window_handles)
driver.switch_to.window(driver.window_handles[2])
print(driver.title)

Output:

First window title = Yahoo

[‘CDwindow-3D8EFAF1BC9A66342F78731C64C802BD’, ‘CDwindow-B6ADD61824FA954B7E52A9844D304C34’]

Second window title = Welcome to the Verizon Media Privacy Policy | Verizon Media Policies

[‘CDwindow-3D8EFAF1BC9A66342F78731C64C802BD’, ‘CDwindow-B6ADD61824FA954B7E52A9844D304C34’, ‘CDwindow-3176B13D77F832E4DEC6869134FECD1D’]

GeeksforGeeks | A computer science portal for geeks

Explanation:

The script does the following:

  1. Opens yahoo and prints the title.
  2. Opens the privacy from yahoo and prints the title.
  3. Opens the new window and opens geeks for geeks on it and prints the title.

Article Tags :