Switching Between Different Windows

A lot of times we are faced with situations, where clicking a link or a button opens up an entire new browser window, and we have multiple instances of windows opening up.

Let’s see a quick example here :

Browse over to this link, and click the hyperlink that says “Open a popup Window“.  A little browser window opens up when we click this hyperlink.

These are multiple windows, and most often than not, they are present in a majority of webpages. If we want to perform some functions in the child window (the one which comes after we click the link), then we need to switch over the control to the child windows and then perform that said operation, because by default,  the focus is on the parent window.

Selenium API has provided us in-built methods, so that we can reset or transfer the focus from the parent to child windows and then perform what we wanna do.

Let us see how these situations can be handled using Selenium.


import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

class GoogleOrgSearch(unittest.TestCase):

       def setUp(self):
           self.driver = webdriver.Firefox()

       def test_google_search_page(self):
           driver = self.driver
           driver.get("http://www.quackit.com/html/codes/html_popup_window_code.cfm")
           str1=driver.title
           print(str1)

           #get the window handles using window_handles( ) method
           window_before = driver.window_handles[0]
           print(window_before)

#since the pop-up is now in an iframe
driver.switch_to.frame(driver.find_element_by_name('result1'))
           driver.find_element_by_link_text('Open a popup window').click()

           #get the window handle after a new window has opened
           window_after = driver.window_handles[1]

           #switch on to new child window
           driver.switch_to.window(window_after)
           str2=driver.title
           print(str2)
           print(window_after)

           #assert that main window and child window title don't match
           self.assertNotEqual(str1,str2)
           print('This window has a different title')

           #switch back to original window
           driver.switch_to.window(window_before)

           #assert that the title now match
           self.assertEqual(str1,driver.title)
           print('Returned to parent window. Title now match')
      def tearDown(self):
           self.driver.close()

if __name__ == "__main__":
unittest.main()

The output should be, the windows name should be printed and title should be asserted as well. Once you run the script, we have this output in PyCharm :

Screenshot from 2015-10-02 11:34:19

Now let us understand how we managed to do it .  The window_handles( ) method provides a way to return all windows in a current session.

We used the switch_to.window( ) method to switch over the control to newly opened window. Here we can pass the following two parameters- either the windows name or

Now here we have assumed that we already know that we have two windows open. What if, there are more than two child windows open. How would you switch the control over to the third or more open windows? I have discussed this more in detail here.

9 thoughts on “Switching Between Different Windows

  1. This will not work.
    It complains about locating by_link_text.
    Don’t confuse reader s and make sure when you write blogs, you are placing an executable program.
    With due respect, I advice you ,please run the test and if and only if it works, otherwise do not confuse readers.

    Thanks,

    Jay

    Like

    1. A short while before, the page changed the HTML and placed the pop up link in an iframe. That is why you are not getting the desired results. I have updated the code, since the page structure before didn’t have had any iframe in it.

      When placing code blocks in my blog, I always make sure that the code block that I am putting is executable. The code that I have put here in my blog are all code, run and then put it in here.

      Instead of just blaming something or cursing the blog writer, may be it would have been beneficial for you to know the root cause of the issue. But wait, you aren’t that type. You would just go about whining about every other single thing.

      I have this executed right now on the system and I have the same output, as I was having before.

      Liked by 1 person

  2. Hi I am getting this error …I am unable to switch to the new window

    52 # switch windows
    —> 53 driver.switch_to_window(driver.window_handles[1])
    54
    55 # wait to make sure the new window is loaded

    IndexError: list index out of range

    Any help will be highly appreciated.

    Like

    1. Sumit, this error means that the index 1 that you’re jumping to is not available by the time the switch command is executed. It may be due to the delay in opening a window. A good idea would be to get the count of the windows before jumping into the windows. Let me know if you want me to help out.

      Like

  3. Ur tuto look like only cover a specific case. How do you switch between two instances of browser for example ? what is an iframe ?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.