There will be times when you will require to perform some testing, where the testing operations open a new browser/tab, a test case may need you to perform some tasks on the newly opened browser window/tab and return to the original window to perform the remaining tasks.
Even if the window/tab is currently on focus but still it is not an active window, so to perform some tasks, you need to switch to a new browser window/tab in selenium python.
Commands will affect the base window unless we switch our control to a new window/tab.
Previously tab and Windows are two different items, as tabs do not have any GU iD. Now developers made tab and windows have GUIDs, so tabs and windows are treated as same in selenium python.
Situations when we are likely to deal with multiple windows:
The above image depicts multiple browser windows
If we perform the above operation manually, we may not find difficulty, but handling the above scenario is tricky in selenium python.
GU ID: An unique string generated for each browser window opened.
8589934593
current_window_handle : returns the current(active) browser window's GU ID string.
driver.current_window_handle;
window_handles : returns GU ID of all the browser windows present at the moment, which are opened by the current driver.
driver.window_handles;
switch_to_window() : switches the control from the current browser window to the target browser window which has the specified GU ID.
driver.switch_to_window(guid);Click the button to open a new window ( google.com)
Please use Firefox as a browser to open the link in a new window; if you use chrome, the link may be opened in a new tab rather than a new window.
1. Open Chrome browser and Navigate to https://chercher.tech/python/windows-selenium-python
# open chrome browser
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
# set implicit time to 30 seconds
driver.implicitlyWait(30);
# navigate to the url
driver.get("https://chercher.tech/python/windows-selenium-python");
2. Get the GU ID of the current (parent) window using the current_window_handle
# get the Session id of the Parent
parentGUID = driver.current_window_handle;
3. Click on the Open New Window button, the application opens a new window with Google page. Make the selenium python sleep for 5 seconds; otherwise, it may not find the newly opened tab.
# click the button to open new window
driver.find_element_by_id("two-window").click();
time.sleep(5000);
4. Get the GU IDs of the two windows (parent + google), using the window_handles present in the selenium python. Store the GU IDs in a Set Collection; this Set will have GU IDs of both parent and Child Browsers
# get the All the session id of the browsers
allGUID = driver.window_handles;
5. iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window
# iterate the values in the set
for guid in all allGUID:
# one enter into if blobk if the GUID is not equal to parent window's GUID
if(guid != parentGUID):
# todo
6. Switch to the window using the switch_to_window() method present in selenium python, pass the GU ID of the child browser to this method.
# switch to the guid
driver.switch_to_window(guid);
7. Find the search bar in Google.com and search for "success."
# search on the google page
driver.find_element_by_name("q").send_keys("success");
Close the Google tab/Window and return to the parent tab/browser window
# close the browser
driver.close();
# switch back to the parent window
driver.switch_to_window(parentGUID);Complete code for switching window may look like below
class TwoWindows(unittest.TestCase) :
def test_browser_Window :
# open chrome browser
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
# set implicit time to 30 seconds
driver.implicitlyWait(30)
# navigate to the url
driver.get("https://chercher.tech/python/windows-selenium-python")
# get the Session id of the Parent
parentGUID = driver.current_window_handle
# click the button to open new window
driver.find_element_by_id("two-window").click()
time.sleep(5000)
# get the All the session id of the browsers
allGUID = driver.window_handles
# print the title of the page
print("Page title before Switching : "+ driver.getTitle())
print("Total Windows : "+allGUID.size())
# iterate the values in the set
for guid in all allGUID:
# one enter into if blobk if the GUID is not equal to parent window's GUID
if(guid != parentGUID):
# switch to the guid
driver.switch_to_window(guid)
# break the loop
break
# search on the google page
driver.find_element_by_name("q").send_keys("success")
# print the title after switching
print("Page title after Switching to goolge : "+ driver.getTitle())
time.sleep(5000)
# close the browser
driver.close()
# switch back to the parent window
driver.switch_to_window(parentGUID)
# print the title
print("Page title after switching back to Parent: "+ driver.getTitle())We have handled the two windows in the above tutorial by comparing GUID, but when we have more than two windows, we cannot use the same approach. To access the exact page, we may need the title of the pages; based on the title of the page; we can differentiate the browser windows in selenium.
Scenario : Click on the Open 3 New Windows button, it will open three more windows (bing, google, yahoo), now switch to Bing and search for "gates".
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python
2. Get the GU ID of the current (parent) window using current_window_handle
3. Click on the Open 3 New Window button, the application opens a new window with the Google page.
4. Get the GU IDs of all windows (parent + google + bing + yahoo), using the window_handles method present in the selenium python and store them.
5. Now iterate through the stored variable, switch to the first GUID in Set, and check the page title contains the keyword "bing".
class MoreThanTwoWindows(unittest.TestCase) :
def test_browser_Window :
# open chrome browser
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
# set implicit time to 30 seconds
driver.implicitlyWait(30)
# navigate to the url
driver.get("https://chercher.tech/python/windows-selenium-python")
# get the Session id of the Parent
parentGUID = driver.current_window_handle
# click the button to open new window
driver.find_element_by_id("three-window").click()
time.sleep(5000)
# get the All the session id of the browsers
allGUID = driver.window_handles
# print number of windows
print("Total Windows : "+allGUID.size())
//iterate through windows
for guid in all allGUID:
driver.switch_to_window(guid)
# check the title of the page to match with "bing"
if("bing" in driver.title.lower()):
driver.findElement(By.className("b_searchbox").send_keys("gates")
time.sleep(3000)
break
# close all the windows
driver.quit()A developer can make the link open in a new window using the target keyword present in anchor tag <a> when the developer sets this value to "_blank"; the link will be opened in new windows when the user clicks the link.
# button is nested inside a link
<a id='two-window' href='https://google.com' target='_blank'><input type='button' value="Open New Window"></a>
# plain link
<a id='two-window' href='https://google.com' target='_blank'>Click me</a>
If the developer avoids using target='_blank', the browser opens such links in the same window. It is recommended not to open the link in the new window
<a id='two-window' href='https://google.com'>Click me</a>There will be situations where we have to open a link in a new window, but later browser does not support opening a URL in a new window as it is not recommended ( phishing is easy with new windows ).
During such scenarios we can use action class to open the link in a new window, by pressing the Shift key and clicking the link, it opens the link in a new window in selenium python.
Steps to open link in a New Window :
1. Open Firefox browser and Navigate to https://chercher.tech/python/windows-selenium-python
# open chrome browser
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
# set implicit time to 30 seconds
driver.implicitlyWait(30);
2. Find the above link using id force-new-window and store it in a variable of type
# store the element
ele = driver.find_element_by_id("force-new-window");3. Create an object for the Actions class
# create object for Actions class
act = ActionChains(driver);
4. Call the key_down method from the Actions class object, and pass Keys.SHIFT as parameter
5. Call Click method from the Actions class object, pass the stored web element as a parameter to this method
6. Use the build() method from the Actions class object to bind the key_down and click methods, call the perform method to perform the operations.
# press the shift key
act.key_down(Keys.SHIFT).click(ele).build().perform();
Complete program for opening a link in a new window in selenium python.
class OpenNewWindow(unittest.TestCase) :
def test_browser_Window :
# open chrome browser
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe')
# set implicit time to 30 seconds
driver.implicitlyWait(30)
# navigate to the url
driver.get("https://chercher.tech/python/windows-selenium-python")
# get the Session id of the Parent
parentGUID = driver.current_window_handle
# store the element
ele = driver.find_element_by_id("force-new-window")
# create object for Actions class
act = ActionChains(driver)
# press the shift key
act.key_down(Keys.SHIFT).click(ele).build().perform()I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.
