Open In App

How to switch to new window in Selenium for Python?

Last Updated : 15 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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-

  • window_handles is used for working with different windows. It stores the window ids that are used for switching.
  • switch_to.window method is used for switching between the windows with the help of window_handles ids.

Steps to cover:

  • Set up the URL and import selenium

Python3




# 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)  


  • Get the website URL and click on the link which opens in a new window. And switch between them.

Python3




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

  • execute_script is a method for passing JavaScript as a string
  • window.open() is a method for opening the new window

Example 1:

Python3




# 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:

Python3




# import modules
from selenium import webdriver
import time
  
# assign path and driver
PATH = "C:/chromedriver.exe"
driver = webdriver.Chrome(PATH)
  
# assign URL
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.


Previous Article
Next Article

Similar Reads

How to Switch between Window using Selenium in Python?
Selenium is an effective device for controlling an internet browser through the program. It is purposeful for all browsers, works on all fundamental OS and its scripts are written in numerous languages i.e Python, Java, C#, etc, we can be running with Python. It's miles fantastically viable that we open a massive range of windows. Each window might
2 min read
How to access popup login window in selenium using Python
Many websites use sign-in using social media to make the login process easy for users. In most cases, if the button is clicked then a new popup window is opened where the user has to enter their user credentials. Manually one can switch windows in a browser and enter the required credentials to log in. But in case of unattended web access using a w
3 min read
Open a new Window with a button in Python-Tkinter
Python provides a variety of GUI (Graphic User Interface) such as PyQt, Tkinter, Kivy and soon. Among them, tkinter is the most commonly used GUI module in Python since it is simple and easy to learn and implement as well. The word Tkinter comes from the tk interface. The tkinter module is available in Python standard library.Note: For more informa
3 min read
PyQtGraph – Getting Window Flags of Plot Window
In this article we will see how we can get window flags of the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) an
3 min read
PyQtGraph – Setting Window Flag to Plot Window
In this article we will see how we can set window flag to the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive graphics for displaying data (plots, video, etc.) and
3 min read
Switch Case in Python (Replacement)
In this article, we will try to understand Switch Case in Python (Replacement). What is the replacement of Switch Case in Python? Unlike every other programming language we have used before, Python does not have a switch or case statement. To get around this fact, we use dictionary mapping. Method 1: Switch Case implement in Python using Dictionary
3 min read
Python | Switch widget in Kivy
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Switch widget: The Switch widget is active or inactive, as a me
5 min read
Python | Switch widget in Kivy using .kv file
Kivy is a platform independent GUI tool in Python. As it can be run on Android, IOS, linux and Windows etc. It is basically used to develop the Android application, but it does not mean that it can not be used on Desktops applications. ???????? Kivy Tutorial - Learn Kivy with Examples. Switch widget: The Switch widget is active or inactive, as a me
4 min read
Create switch with Python-kivymd
In this article, we will see how to add the switch in our application using KivyMD in Python. MDSwitch: The switch is a kind of toggle button mostly used in android apps to on/off features.Its looks as: Installation: To install the modules type the below command in the terminal. pip install kivy pip install kivymd Method 1: Using kv language: Step
4 min read
Alternatives to Case/Switch Statements in Python
In many programming languages, the case or switch statement is a control flow mechanism that allows a variable to be tested for equality against a list of values, with each value associated with a block of code to be executed. However, Python does not have a built-in case or switch statement. Instead, Python developers use various alternative struc
3 min read
Practice Tags :