How to open new window Selenium Python

In this post, we are going to learn how to open new window Selenium Python with code examples. The Selenium web driver API is used for test automation on Windows or mobile, It helps us remotely control the 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, select values from the dropdown, mouse control.

1. How to open new window Selenium Python


In this example, we will learn how to open new Windows Selenium Python, First, we will load a chrome browser driver with the help of ChromeDriverManager that automatically handle 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 driver.get(“https://accounts.google.com/”) method to navigate to the link given as a parameter. it will open the yahoo login page and find the element by the link text “help” and the click() event is used to navigate to the privacy page.

  • driver.get(): Navigate to a new link that is given as parameter
  • window_handles: The selenium method is used for working with different windows. It stores window GUID that are help switch between windows.
    • switch_to.window: It used the window_handles ids to move between the window and switch focus on a particular window.
  • sleep(3): wait 3 seconds until a new tab opens
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  = Yahoo
The Second opened  window   = Welcome to the Yahoo Privacy Policy | Yahoo

2. How to open new window Selenium Python


To open a new window in selenium python, First, we have navigated to the link by using driver.get(“https://accounts.google.com/”) method 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])

#get the new URL
driver.get("https://www.duckduckgo.org/")

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

driver.close()
driver.quit()

Output

First Window URL = Yahoo
Second  Window URL  = Welcome to the Yahoo Privacy Policy | Yahoo

Summary

In this post we have learned how to open new window Selenium Python with examples by using different method in selenium driver api ,driver.get(),driver.switch_to.window(),current window handle:,sleep(2),driver.execute_script(“window.open()”)