Design a site like this with
Get started

How do you handle frames and windows in Selenium?

When selenium testing is being performed, there will be situations where frames or windows need to be handled strategically. The content of a particular webpage is segmented into vertical and horizontal splits. Creating robust locators in selenium is one of the significant challenges when such types of applications are worked upon because eventually, the elements on the page need to interact. When frames are used, it becomes even more difficult, as frames have an issue with proper interaction. When a web application or website is automated, there will be scenarios where multiple windows will be opened within an application, when a user performs certain actions on all the windows that have been opened. There should be a specific mechanism to handle all the windows in selenium. In this article, you will get to know about handling windows and frames in windows through the application of certain specific methods.

What does a frame in Selenium mean?

Frames provide a platform where sections of a web page are divided using HTML.

Following are the methods that can be used to handle frames in Selenium:

1. switchTo().frame(framenumber): The frame id is used as the parameter. The frame id index starts from 0. If the frame is not found then NoSuchFrameException is thrown.

2. switchTo().frame(frameName): The parameter for frame name is used as defined by the developer. The frame name is enclosed in quotes and is considered to be a string. If the frame is not found then NoSuchFrameException is thrown.

3. switchTo().frame(WebElement): The webelement is used as the parameter. If the frame is not found then NoSuchFrameException is thrown. If the frame is no longer active then StaleElementReferenceException is thrown.

3. switchTo().defaultContent(): With this method, the focus is shifted to the first frame of the page.

What does a window in Selenium mean?

It is the main webpage on which a user lands by clicking on a specific URL/Link. Such a window is known as the parent window in Selenium and in other words, it is known as the main window. When the Selenium WebDriver session is created, it gets opened.

Identifying parent window and child window:

A webpage is opened when a user hits a URL. This page is known as the parent window, i.e., the main window on which the user has landed by clicking on the specific URL. When the selenium automation script gets executed, this is the same webpage that gets opened. Inside the main window, all the windows that get opened are known as child windows. Depending upon the context, a child window may or may not have a URL. 

The behavior of the child windows can be checked easily when a web application is being manually tested. This is because they are visible in the context of the main window.

Following are the methods that can be used to handle windows in Selenium:

1. getWindowHandle(): The main window i.e., the parent window that uses getWindowHandle() method needs to be handled when a website opens. A unique ID of the current window is provided by this method that will identify it through the driver instance. The value of the string type will be returned. 

2, getWindowHandles(): All the opened windows, which are the child windows are handled by the web driver by using a driver.getWindowHandles() method. 

3. Set: The window is set in the form of a string. The syntax is as follows:


4. switchto(): Using this method, windows can be switched 

5. action: Using this method, certain actions can be performed on the windows. 
Conclusion: If you are looking forward to implementing selenium testing for your specific project, then do get connected with a well-established software testing services company that will provide you with methodical testing solutions and tactical advice that is in line with your project specific requirements


Leave a comment

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

You are commenting using your 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

%d bloggers like this: