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, driver.switchTo().frame(…);

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
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);

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

That’s it about the brief, now let’s see the code implementation
——————————————————-
We will try to access the “Category1” link (inside an iframe) on website www.qavalidation.com demo as a normal webelement and see what happens

OutPut

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

 

Content posted is based on learning or working experience, please leave comments if anything needs to be added or updated, discuss your queries on our facebook:qavalidation.com, Thanks!

Leave a Reply

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

%d bloggers like this: