List of Windows

This property is used to get the values of all the windows which are opened in the current session by WebDriver in alphanumeric.It doesn’t take any argument but it returns a set object because set doesn’t allow duplicate values in it.

Using for loop we can iterate the set object to print or to perform action on the required window.

 
driver.window_handles

close() method :

This method is used to close only the current browser window (webpage) which is opened in session and it will don’t close all the browser windows which are opened.It doesn’t take any argument and it doesn’t return anything.

 
Syntax: driver.close()



quit() method :

This method closes all the browser windows which are opened in session.It doesn’t take any argument and it doesn’t return anything.

 
Syntax: driver.quit()

Example:

  • Launch the webpage "http://www.dummypoint.com/Windows.html"
  • Get the current window name
  • click on popup button to open new window
  • Using for loop iterating all the input tag elements and clicking on required tag by its value
  • Print the list of windows that are present on the screen in the present session.
  • Wait for 5 sec.
  • Close the Web Browser

List_of_Window.py

from selenium.common.exceptions import ElementNotVisibleException, ElementNotSelectableException, NoSuchElementException
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium import webdriver
import time

driver = webdriver.Chrome("/Users/admin/Documents/Skill2Lead/Others/drivers/chromedriver.exe")  # Using Chrome Driver

# 1. Launch the webpage
driver.get("http://www.dummypoint.com/Windows.html")
assert "Selenium Template" in driver.title

# 2. To get the current window name
window_name = driver.current_window_handle
print("Before clicking popup button ", window_name)

time.sleep(2)

#3.  click on popup button to open new window
windows_popup = driver.find_elements(By.TAG_NAME,"input")

# 4. Using for loop iterating all the input tag elements and clicking on required tag by its value
for popup_bs in windows_popup:
    popup_b = popup_bs.get_attribute("value")
    if popup_b == "Open a Popup Window2":
        popup_bs.click()

time.sleep(2)

#5.  Print the list of windows that are present on the screen in the present session.
windows = driver.window_handles
for window in windows:
    print(window)

time.sleep(5)
driver.quit()