Handling iframes in selenium

There are situations where web controls/elements reside inside an iframe.

iframe can be represented in html as tag name=”iframe”.

Elements inside an iframe can not be accessed as normal selenium statement – driver.findelement(By…)

need to use,


In selenium, how we can identify if web element[s] is/are inside an <iframe …=””> </iframe> ???
Right click on an web element, check if the context menu has any one of the following options –

If you get the options like above screenshot, then there are ways we can identify the web elements-

Individual iFrames

driver.switchTo().frame(int x); //by index, if  one or more frames present
driver.switchTo().frame(String frameName); //frame by name
driver.switchTo().frame(WebElement element); //frame by locator

Nested iFrames

If there are nested iframes, then we can use

Out of iFrames

Once complete the action on the iframe elements, if we want to come out of the frame, then do
driver.switchTo().defaultContent(); //to come out of a frame for normal run

Code implementation

We will try to access the text “I am inside frame” text in a normal way (though it’s inside an iframe)


Now let’s try to access the iframe elements by using driver.switchTo().Frame…

Watch demo here

Content posted is based on learning or working experience,
Share the post with others if find useful,
Any queries?, leave comments or discuss on our facebook:qavalidation.com, Thanks!

Leave a Reply

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