Work with multiple browser windows

In the previous post,we have seen how to enter data into edit boxes and click on button. In this post ,we will see how to work with multiple browser windows.

This situation may come in any of your application, that when you click on any link or button , it will open a new browser window where you need to perform some desired tasks. We will see how to handle those situation.

Let’s take example from our demo application. We have 3 links over there , and if we click at the link ‘Learn Selenium!!’, it will open into a new window. It opens a Selenium Tutorials page. We will verify the title of the page to make sure that it has switched to the child browser. After that we will close this child browser and switch to the parent browser.

To verify that it has again switched to the parent browser, we will perform some activity on the parent browser e.g. let’s enter name in first name.

Link click

Output: Title : Selenium Tutorials | Testingpool

It has switched to the child browser which is Selenium Tutorials page as we can see it prints the title of the page successfully. It has switched to the parent window successfully, enter name in the edit box ‘First Name’.

Enter Name

Explanation of Function:

  • Open application and create a webelement named ‘link’ (Selenium learning!!)
  • Click on the link , it will open another browser window
  • getWindowHandles() :  This method returns browser window handles. Each browser handle is unique , with which selenium identifies the desired browser. It returns a set of Strings.
  • Iterator(): Iterate over the collection of window handles and provide parent window handle and child window handle.
  • switchTo(): This method helps to switch to parent to child window and vice versa.
  • driver.getTitle(): It will return the title of the current focused browser window.
  • driver.close(): Closing child browser.

 

Print Friendly, PDF & Email
Enter text in a edit box and click on a button
Handle cookies in Selenium WebDriver
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *