How To Handle Iframes

iFrame:

An iFrame is simply a Frame or Inline Frame, where an HTML document loads another webpage within the HTML document. iFrame is the most commonly used HTML Element to display or embed a complete web page into a new window.

How to Handle the iFrame Element in Selenium?:

To identify and perform the actions on elements inside the iFrame selenium provides a special comman driver.switchTo().frame().

To handle elements inside iFrame we need to switch focus of selenium to iFrame. Selenium provides multiple ways to switch over to iFrame.

1. switchTo.frame(int frameNumber)
2. switchTo.frame(string frameIdOrName)
3. switchTo.frame(WebElement frameElement)

Example: 

Consider this HTML Code for the reference.

<div id=”modal”>

  <iframe id=”buttonframe” name=”myframe”  src=”https://seleniumhq.github.io”>

   <button>Click here</button>

 </iframe>

</div>

1. Switch to iFrame in Selenium using Index:

An Index number represents the position of a specific iframe on an HTML page.
Example-

WebDriver driver = new ChromeDriver(); 
driver.get("URL having iframes");   
//Switch by Index 
driver.switchTo().frame(1);

2. Switch to iFrame in Selenium using Name Or ID:

The most common and efficient way to switch to iFrame in selenium is using the frame id or name. Where iFrame is associated with id or name.
Example-

To switch between an iframe using the Id attribute:

WebDriver driver = new ChromeDriver();
driver.get("URL having iframes"); 
//Switch by frame id
driver.switchTo().frame("buttonframe"); 

To switch between an iframe using the name attribute:

WebDriver driver = new ChromeDriver();
driver.get("URL having iframes"); 
//Switch by frame name
driver.switchTo().frame("myframe"); 

3. Switch to iFrame in Selenium using WebElement:

Another way to switch between frames in selenium is to pass the WebElement to the switchTo() command.
Example-

WebDriver driver = new ChromeDriver();
driver.get("URL having iframes"); 
//Finding the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("buttonframe"));
//now using the switch command
driver.switchTo().frame(iframeElement);

Switching between iFrames and Main Page in Selenium:

The parenetFrame() in Selenium:

Syntax
driver.switchTo().parentFrame();

The driver.switchTo().parentFrame(); always switches to the immediate parent frame depending on the frame you are working in.
For Example- Suppose the Main window has iFrame1, the iFrame 1 as another iFrame in it that is iFrame 2. if you are inside the iFrame2, then driver.switchTo().parentFrame();, switches the context to iFrame1.

The defaultContent(); in Selenium:

Syntax
driver.switchTo().defaultContent();

Regardless of your current context, you can switch to the main page using the driver.switchTo().defaultContent(); switches the context back to the main page automatically.
For Example- If you are inside the iFrame2, then driver.switchTo().parentFrame();, switches the context to main window.

#How_to_handle_iframe_in_selenium #iFrame #Switching_between_iFrames_and_Main_Page_in_Selenium #parenetFrame #defaultContent_methods #driver.switchTo().frame()

(New page will open, for Comment)

Not yet commented...