Before talking about Handle Iframes in Selenium, you should know what is Iframes.
What is Iframe?
A web page or an HTML document embedded inside another web page or HTML document is called as a frame. An iframe is usually used to insert contents from other sources to a website.
How to Identify the Iframe
Frames cannot be detected just by seeing the page or by inspecting Firebug. You need to use the following methods to identify Iframes.
- If you find an option “This frame” when you right click on the element, it is an Iframe.
- Right-click on the page and go to “View Page Source”. Then start searching with “Iframe”. If any tag name can be found with Iframe, you can make sure that the web page has Iframes.
How to switch over the elements in Iframes using Webdriver Commands
Basically, there are three ways to switch over the elements in Iframes using webdriver commands.
- By Index
- By ID or Name
- By Web element
Once the action is over, we can move back to the parent frame by “Switch To() Parent Frame()” command and if you want to go back to the main frame (Most parent) you can use “Switch to() Default Content().
In some cases, there will be a number of frames in the page and it will not have an ID. In such case, it will be difficult to find the frame from which the required element is loaded. In such case, the index of the element should be found and this can switch to the IFrame through the index.
Concept of Nested Frames
Just imagine there are two Iframes, one inside the other and the action supposed to be performed is to print the content in both the frames. In the case of nested frames, at first, we should switch to the outer frame either by id or index. Once in the outer frame, the number of inner frames will be known. Now switch to the inner frame one by one using any of the known methods. While exiting you can perform the same procedure in the reverse order. Switching from innermost frame to the outermost frame one by one.
When you use selenium for testing web based applications, you will have to handle Iframes in many occasions. Hence, you should have a clear understanding about Iframes and handling of Iframes. This portion of the subject will be taught in detail in all selenium tutorials and will be given importance in Selenium live project training also.