How to switch windows in Selenium Python

In this post, we are going to learn How to switch windows in Selenium Python. The Selenium web driver API is used for test automation on Windows or mobile. It helps us remotely control different browsers automatically as if a real user is interacting by stimulating activities like opening or closing a new window, or tab, entering text in the textbox, selecting values from the dropdown, and mouse control.

Methods used to switch windows


  • window_handles: The selenium method is used for working with different windows. It stores window GUID which is helpful for switching between windows.
  • switch_to.window: It used the window_handles GUID to move between the window and switch focus on a particular window.

1. How to switch windows in Selenium Python


In this example, we will learn how to open new Windows using Selenium Python. First, we will load a chrome browser driver with the help of ChromeDriverManager which automatically handles install of a new chrome browser driver, whenever the new version of the driver is released. To open a new window in selenium python, First, we have to use the driver.get(“https://login.yahoo.com/”) method to navigate to the link given as a parameter. it will open the yahoo login page and find the element by the class name “privacy” and the click() event is used to navigate to the privacy page.

  • driver.get(): Navigate to a new link that is given as a parameter.
  • driver.switch_to.window: moving between the window and switching focus on a particular window.
    • window handle: It stores window GUID that are help switch between windows.
  • sleep(3): wait some time until a new tab opens, after that, we will navigate to new window.
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.maximize_window()

driver.get("https://accounts.google.com/") 



print("The First opened window  = " + driver.title)

driver.find_element(By.LINK_TEXT,"Help").click()

time.sleep(3)  
  

driver.switch_to.window(driver.window_handles[1])

print("The Second opened  window   = " + driver.title)  

driver.switch_to.window(driver.window_handles[0])
  

print(driver.window_handles)  

Output

The First opened window  = Sign in - Google Accounts
The Second opened  window   = Google Account Help
['CDwindow-73D4B113CD1026EE95395DA7D21D5996', 'CDwindow-3025DCF240969A8CD46C2AB0C4A0BAC0']

2. How to switch windows in Selenium Python


To switch windows in selenium Python, First, we have navigated to the link by using the driver.get(“https://accounts.google.com/”) method which will open the yahoo login page and find the element by the link name “help” and the click() event is used to navigate to the privacy page.

  • driver.get(): Navigate to new link
  • driver.switch_to.window: moving between the window and switch focus on a particular window.
    • current window handle: It stores window GUID that are help switch between windows
  • sleep(2): for wait some time until a new tab opens
  • driver.execute_script(“window.open()”) : Used to execute Javascript command that used to open new window.
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()

driver.get("https://accounts.google.com/") 



print("The First opened window  = " + driver.title)

driver.find_element(By.LINK_TEXT,"Help").click()
  

time.sleep(2)  
  

driver.switch_to.window(driver.window_handles[1])

print("Second  Window URL   = " + driver.title)

#switch to old window
driver.switch_to.window(driver.window_handles[0]) 


driver.execute_script("window.open()")
driver.switch_to.window(driver.window_handles[2])

#switch to new window
driver.get("https://www.duckduckgo.org/")

print(driver.title)
time.sleep(2) 

driver.close()
driver.quit()

Output

The First opened window  = Sign in - Google Accounts
Second  Window URL   = Google Account Help
DuckDuckGo — Privacy, simplified.