Handle iFrame in selenium:

 

What is Iframe?

  • Iframe is a web page embedded in another web page or an HTML document is embedded inside another HTML document.
  • Iframe is often used to insert content from any other source, such as advertising, in a web page.
  • The <iframe> tag specifies an inline frame.
  • An inline frame is used to embed another document in the current HTML document.
  • This means that iframe is actually a webpage inside the webpage on which the page has its own domicile for every iframe.

 

Sample Code of iFrame:

 

Identification of a iframe on WebPage:

  1. Right-click on the element. Check if “This Frame” option is available. It means this is a iFrame.
  1. Right click and click on Inspect then View page source of the web page and check if  “iFrame” tag is Available or Not?

 

Switch to iFrame using Multiple ways:

  1. By Index
  2. By Name or Id
  3. By Web Element

 

You need to first find iFrame. You can do so using following statement:

  • Find iFrame with tagName and store into WebElement to Perform operation on it.

 

1. By index, if  one or more frames present:

  • Using switchTo Method we can Move / Switch WebDriver Instance / Object from one frame to other using index of iFrame.

 

2. By Name or ID:

  • Using switchTo Method we can Move / Switch WebDriver Instance / Object from one frame to other using Frame Name of iFrame.

 

3. By WebElement locator:

  • Using switchTo Method we can Move / Switch WebDriver Instance / Object from one frame to other using WebElement Locator of iFrame.

 

Switch to one frame to another Frame:

  • Using switchTo Method using Two times we can switch from one iFrame to other iFrame.

 

Switch to Main / Parent Frame:

  • After perform operation on iFrame we have need to Move back into Parent frame that time we switchTo into parentFrame / defaultContent.

 

Scenario’s To Handle iFrame:

  1. Inspect if the element is within an iFrame.
  2. Switch to the iFrame.
  3. Identify and then perform the required operation on the element.
  4. Switch back to main web page.

Example of Handle iframe in Selenium: