How to handle multiple windows in Selenium Web driver
In automation, when we have multiple windows in any
web application, the activity may need to switch control among several windows
from one to other in order to complete the operation. After completion of the
operation, it has to return to the main window i.e. parent window.
To uniquely identify an opened browser Selenium
WebDriver keeps a map of Opened windows VS Window Handle. Window handle is a
unique string value that uniquely identifies a Browser window on desktop. Each
browser will have a unique window handle. To get Window handle WebDriver
provides two methods.
current_window_handle and window_handles
current_window_handle: This method return a string value and it returns the
Window handle of currently focused browser window.
Window_handles: This method returns a set of all Window handles of all
the browsers that were opened by the web driver.
Sceanrio
1:
Open Chrome browser
Get current window handle and print it to console
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
# below is the
method for get the current window handle ID
ParentWindow=driver.current_window_handle
print("Patnet
window Id : ",ParentWindow)
Sceanrio
2:
Open Chrome browser
Get current window handle and print it to console
Locate ‘Brief On Selenium Grid’ Link using XPath
Click on Link , it will open the new window
Get the window handles of both the open windows
Loop through both handles
Switch to the new window with the handle’s reference
Get the title and print it to console
Close the new window
Switch the control back to parent window and print
the title of the parent window to the console
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
ParentWindow=driver.current_window_handle
driver.find_element_by_xpath("//a[contains(@href,'brief-selenium-grid')]").click()
print("Patnet window Id : ",ParentWindow)
driver.find_element_by_xpath("//a[contains(@href,'brief-selenium-grid')]").click()
print("Patnet window Id : ",ParentWindow)
windowHandleList=[]
windowHandleList=driver.window_handles
for windowhandle in windowHandleList:
if windowhandle != ParentWindow:
driver.switch_to.window(windowhandle)
childwindwtitle=driver.title
print(childwindwtitle)
driver.close()
driver.switch_to.window(ParentWindow)
parentwinddowtitle=driver.title
print(parentwinddowtitle)
windowHandleList=driver.window_handles
for windowhandle in windowHandleList:
if windowhandle != ParentWindow:
driver.switch_to.window(windowhandle)
childwindwtitle=driver.title
print(childwindwtitle)
driver.close()
driver.switch_to.window(ParentWindow)
parentwinddowtitle=driver.title
print(parentwinddowtitle)
In above script we are using List because we need collect opened
window handle id.
I used ‘for’ loop and ‘if’ condition because I want to switch over
to child window, so we need to compare with parentwindowHandleID.
By default the focus is always on the Parent window, In order to
shift focus from Parent Window to any child window we have to use the following
command on WebDriver – driver.switch_to.window(windowhandle).
you can use all web driver command on child window once you switch
over to child window.
Note: without switching to the child window we will not be able to
perform any action on that window and you will get “selenium.common.exceptions.NoSuchWindowException”.
After closing a Child window you have to explicitly switch to Parent
window before sending in any WebDriver commands.
In the above code we will close the Child window and then
explicitly w
Switch to the parent window.
Scenario 3:
Open Chrome browser
Get current window handle and print it to console
Locate ‘Brief On Selenium Grid’ Link using XPath
Click on Link , it will open the new window
Get the window handles of both the open windows
Loop through both handles
Switch to the new window with the handle’s reference
Get the title and print it to console
Don’t close child window.
Switch the control back to parent window and print the URL to the
console.
Close all open browsers
Script:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
import time
chromePath="C:\\BrowserDrivers\\chromedriver.exe"
driver=webdriver.Chrome(executable_path=chromePath)
driver.get("http://trendnxt.blogspot.com/p/selenuim.html")
ParentWindow=driver.current_window_handle
driver.find_element_by_xpath("//a[contains(@href,'brief-selenium-grid')]").click()
print("Patnet window Id : ",ParentWindow)
driver.find_element_by_xpath("//a[contains(@href,'brief-selenium-grid')]").click()
print("Patnet window Id : ",ParentWindow)
windowHandleList=[]
windowHandleList=driver.window_handles
for windowhandle in windowHandleList:
if windowhandle != ParentWindow:
driver.switch_to.window(windowhandle)
childwindwtitle=driver.title
print(childwindwtitle)
driver.switch_to.window(ParentWindow)
parentwinddowtitle=driver.title
print(parentwinddowtitle)
windowHandleList=driver.window_handles
for windowhandle in windowHandleList:
if windowhandle != ParentWindow:
driver.switch_to.window(windowhandle)
childwindwtitle=driver.title
print(childwindwtitle)
driver.switch_to.window(ParentWindow)
parentwinddowtitle=driver.title
print(parentwinddowtitle)
# close all open
browsers
driver.quit()
driver.quit(): will close all the windows opened by the web driver. This command
shuts down the driver instance and any further commands to WebDriver results in
exception.
Note: without switching to the any window(other than parent) we
will not be able to perform any action on that window and you will get “selenium.common.exceptions.NoSuchWindowException”.
Comments
Post a Comment