We can handle multiple windows in selenium webdriver using Switch To methods which will allow us to switch control from one window to another window.If you are working with web applications then you must have faced this scenario where you have to deal with multiple windows.
If you have to switch between tabs then also you have to use the same approach. Using switch To method we can also handle frames and alerts with easy methods. I will focus on multiple windows as of now.
Before starting this section I will recommend you watch List and Set in Java which will help you to understand this concept in details.
In Selenium, we have the feature that we can get the window name of the current window.In Selenium, we have the getWindowName method that will return current window name in String form.
Approach to handle multiple windows in selenium webdriver
We also have getWindowNames, which will return Set<String> it means the set of window name then we can iterate using Iterator. The set is part of Java collection which allows us to handle multiple sets of data dynamically.
Scenario 1- In some application, you will get some Pop up that is nothing but separate windows takes an example of qaearth practice page.
Therefore, in this case, we need to close the popup and switch to the parent window.
First, we will switch to child window (pop up window) then we will close it and then we will switch to the parent window.
Instead of Set , we can use List as well as shown in below snippet.
If you have to switch between tabs then also you have to use the same approach. Using switch To method we can also handle frames and alerts with easy methods. I will focus on multiple windows as of now.
Before starting this section I will recommend you watch List and Set in Java which will help you to understand this concept in details.
In Selenium, we have the feature that we can get the window name of the current window.In Selenium, we have the getWindowName method that will return current window name in String form.
Approach to handle multiple windows in selenium webdriver
We also have getWindowNames, which will return Set<String> it means the set of window name then we can iterate using Iterator. The set is part of Java collection which allows us to handle multiple sets of data dynamically.
Scenario 1- In some application, you will get some Pop up that is nothing but separate windows takes an example of qaearth practice page.
Therefore, in this case, we need to close the popup and switch to the parent window.
Complete program to handle multiple windows in selenium webdriver
Instead of Set , we can use List as well as shown in below snippet.