Handling Iframes in Selenium

Posted By : Ankita Sachan | 30-Oct-2017

Iframe is a HTML document inherited inside HTML document defined by tags </iframe></iframe> in DOM because of tags present the iframes can be easily identified.

 

 

For Example:

 

 

 

To switch in between the iframes we can use switchTo().frame command. This command can be used in three ways.

 

  • switchTo.frame(int frameNumber): Pass the frame index and driver will switch to that frame.

  • switchTo.frame(string frameNameOrId): Pass the frame element Name or ID and driver will switch to that frame.

  • switchTo.frame(WebElement frameElement): Pass the frame web element and driver will switch to that frame.


 

Switch to frame by index

 

Index for an iframe will be the position at will it will be situated in the HTML page. In the example given below we are having two frames index for both the frames are 0 and 1.


 

To switch in between there two iframes is as follows:

 

  

 

Switch to frames By Name

 

Looking into the HTML code we come across that their are name attribute assigned to to each iframe. Name attribute have value iframe1 . so for switching in between frames we can use

driver.switchTo().frame(“iframe1”).

For example

 

        

 

Switch to frame by ID

 

Apart from the name attribute in iframe tag ID attribute is also included. Which can be easily used to switch in between frame. We just have to pass the ID in our code.

For example:




 

        

 

Switch to frame by WebElement

 

We can even use the WebElement to switch in between frame for that we simply have to pass the iframe webelement to driver.switchTo().frame() command.

For example:

 

        

 

Switching back to Main page from frame

 

Switching back to main page is an important task to be done to continue with the rest script. Main page is the page where 2 or more iframes are embedded once we are done with all the iframes we can stick back to the main page using the command switchTo().DefaultContent()

 

        

 

Hence all the iframes can be easily handled by above methods.

Related Tags

About Author

Author Image
Ankita Sachan

Ankita is a Bright QA engineer and have experience in Manual and Automation Testing. loves to Travel to different places.

Request for Proposal

Name is required

Comment is required

Sending message..